Extract loginSuccess to login.go

This commit is contained in:
2016-11-23 23:18:13 +01:00
parent aa055e944e
commit bd1b3416b4
2 changed files with 32 additions and 30 deletions

29
main.go
View File

@@ -2,17 +2,10 @@ package main
import (
"net/http"
"time"
"github.com/dgrijalva/jwt-go"
"gopkg.in/gin-gonic/gin.v1"
)
const (
expiration = 72
secret = "uditapbzuditagscwxuqdflgzpbu´ßiaefnlmzeßtrubiadern"
)
func main() {
router := gin.Default()
@@ -74,25 +67,5 @@ func loginPost(c *gin.Context) {
return
}
// Create token
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"usr": "jan",
"name": "Jan Bader",
"exp": time.Now().Add(time.Hour * expiration).Unix(),
})
// Generate encoded token and send it as response.
t, err := token.SignedString([]byte(secret))
if err != nil {
c.AbortWithStatus(http.StatusUnauthorized)
}
maxAge := (int)((expiration * time.Hour).Seconds())
c.SetCookie("authentication", t, maxAge, "", "", false, true)
c.JSON(http.StatusOK, map[string]string{
"token": t,
})
return
loginSuccess(c, username, "Jan Bader")
}