Skip to content

3380issei/gin-jwt-auth-sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

詳細はこちら

https://qiita.com/fghyuhi/items/ff96c688798bb633be59

やりたいこと

Ginでユーザーログイン機能を実装し、ログインユーザーだけがアクセス可能なエンドポイントをJWT認証を使って実装したい。

main.go

func main() {
	r := gin.Default()

	r.POST("/login", loginHandler)

	authGroup := r.Group("/auth")
	authGroup.Use(authMiddleware)
	authGroup.GET("/", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{"message": "you are authorized"})
	})

	r.Run(":8080")
}
  • POST /login:ログイン時にアクセスするエンドポイント
  • GET /auth:ログインユーザーしかアクセスできない

/authが付くエンドポイントをグループ化し、middlewareを用いて、ログインユーザーのみアクセスできるようにしてます。

参考

https://developer.mamezou-tech.com/blogs/2022/12/08/jwt-auth/

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages