feat: add AuthRefresh in controller
parent
797cd67546
commit
0251c84bfd
|
@ -7,22 +7,20 @@
|
|||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/fbonhomm/api-go/source/config"
|
||||
"github.com/fbonhomm/api-go/source/libs"
|
||||
"github.com/fbonhomm/api-go/source/models"
|
||||
"github.com/fbonhomm/api-go/source/services"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Login
|
||||
// AuthLogin
|
||||
func AuthLogin(c *gin.Context) {
|
||||
user := models.User{
|
||||
Email: c.PostForm("email"),
|
||||
}
|
||||
|
||||
if err := config.Db.First(&user).Error; err != nil {
|
||||
if err := services.Db.First(&user).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
@ -31,26 +29,43 @@ func AuthLogin(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
token := jwt.New(jwt.SigningMethodES256)
|
||||
refresh := jwt.New(jwt.SigningMethodES384)
|
||||
|
||||
claims := token.Claims.(jwt.MapClaims)
|
||||
claims["id"] = user.ID
|
||||
claims["name"] = user.Name
|
||||
claims["email"] = user.Email
|
||||
claims["exp"] = time.Now().Add(time.Minute * 15).Unix()
|
||||
|
||||
accessToken, err := token.SignedString(services.PrivateKeyAccess)
|
||||
accessToken, err := libs.GenerateAccessToken(user)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error() })
|
||||
return
|
||||
}
|
||||
|
||||
claims = refresh.Claims.(jwt.MapClaims)
|
||||
claims["id"] = user.ID
|
||||
claims["exp"] = time.Now().Add(time.Hour * 24).Unix()
|
||||
|
||||
refreshToken, err := refresh.SignedString(services.PrivateKeyRefresh)
|
||||
refreshToken, err := libs.GenerateRefreshToken(user)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error() })
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"access_token": accessToken,
|
||||
"refresh_token": refreshToken,
|
||||
})
|
||||
}
|
||||
|
||||
// AuthRefresh
|
||||
func AuthRefresh(c *gin.Context) {
|
||||
user := models.User{}
|
||||
|
||||
token, _ := c.Get("Token")
|
||||
info := token.(map[string]string)
|
||||
|
||||
if err := services.Db.First(&user, info["id"]).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
accessToken, err := libs.GenerateAccessToken(user)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error() })
|
||||
return
|
||||
}
|
||||
|
||||
refreshToken, err := libs.GenerateRefreshToken(user)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error() })
|
||||
return
|
||||
|
|
Loading…
Reference in New Issue