{"id":13794077,"url":"https://github.com/yasaricli/gah","last_synced_at":"2025-04-10T10:31:08.844Z","repository":{"id":53502294,"uuid":"231647116","full_name":"yasaricli/gah","owner":"yasaricli","description":"Gin Auth Handlers","archived":false,"fork":false,"pushed_at":"2021-03-27T19:32:08.000Z","size":26,"stargazers_count":19,"open_issues_count":3,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-05-23T05:00:44.399Z","etag":null,"topics":["api","authentication","go","go-api","golang","mongodb"],"latest_commit_sha":null,"homepage":"https://dev.to/yasaricli/authentication-with-gin-gonic-gah-for-golang-29d1","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yasaricli.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-01-03T18:56:10.000Z","updated_at":"2024-05-23T05:00:43.764Z","dependencies_parsed_at":"2022-09-06T01:00:27.746Z","dependency_job_id":null,"html_url":"https://github.com/yasaricli/gah","commit_stats":null,"previous_names":["yasaricli/gin-auth-handlers"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yasaricli%2Fgah","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yasaricli%2Fgah/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yasaricli%2Fgah/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yasaricli%2Fgah/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yasaricli","download_url":"https://codeload.github.com/yasaricli/gah/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248199136,"owners_count":21063641,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","authentication","go","go-api","golang","mongodb"],"created_at":"2024-08-03T23:00:35.486Z","updated_at":"2025-04-10T10:31:08.515Z","avatar_url":"https://github.com/yasaricli.png","language":"Go","funding_links":[],"categories":["Middlewares"],"sub_categories":[],"readme":"# Gin Auth Handlers\n\n### Installation\nTo install **Gah (Gin Auth Handlers)** package, you need to install Go and set your Go workspace first.\n\n#### Related posts\n\n**dev.to**\n[Authentication with gin-gonic \u0026\u0026 gah for golang](https://dev.to/yasaricli/authentication-with-gin-gonic-gah-for-golang-29d1)\n\n\nYou can use the below `Go` command to install **Gah**.\n\n    go get -u github.com/yasaricli/gah\n\n### Usage and documentation\n\nFirst let's set the required `environment variables`\n\n```bash\nexport MONGO_URL=mongodb://127.0.0.1:27017 # MongoDB server URL.\nexport MONGO_DATABASE=project_db # MongoDB Project db name\nexport MONGO_COLLECTION=users # Collection to register all users\n```\n\n#### Use with gin-gonic\n\nYou need the **gin** package to use **gah**. You can install it as follows.\n\n    go get -u github.com/gin-gonic/gin\n    \nAdd the `LoginHandler` and `RegisterHandler` functions to the **API**.\n\n```golang\npackage main\n\nimport (\n  \"github.com/gin-gonic/gin\"\n  \"github.com/yasaricli/gah\"\n)\n\nfunc main() {\n  router := gin.Default()\n   \n  api := router.Group(\"/api\")\n  {\n    api.POST(\"/login\", gah.LoginHandler)\n    api.POST(\"/register\", gah.RegisterHandler)\n  }\n\n  router.Run(\":4000\")\n}\n```\n\n#### AuthRequiredMiddleware\n\nLet's make a handler where the user is required:\n\n**Default: Get user ID and auth token from X-User-Id and X-Auth-Token headers**\n\n```golang\nfunc main() {\n  router := gin.Default()\n   \n  api := router.Group(\"/api\")\n  {\n    api.GET(\"/books\", gah.AuthRequiredMiddleware, func(c *gin.Context) {\n      userID := c.MustGet(\"userID\")\n      \n      c.JSON(200, gin.H{\n        \"userId\": userID,\n      })\n    })\n  }\n\n  router.Run(\":4000\")\n}\n```\n\n#### Production Release Mode\n\n```sh\n[GIN-debug] [WARNING] Running in \"debug\" mode. Switch to \"release\" mode in production.\n - using env:\texport GIN_MODE=release\n - using code:\tgin.SetMode(gin.ReleaseMode)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyasaricli%2Fgah","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyasaricli%2Fgah","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyasaricli%2Fgah/lists"}