-
Notifications
You must be signed in to change notification settings - Fork 5
/
verify_user_controller.go
47 lines (40 loc) · 1.29 KB
/
verify_user_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package authentication_controller
import (
"github.com/gin-gonic/gin"
"github.com/gocms-io/gocms/utility/api_utility"
"github.com/gocms-io/gocms/utility/errors"
"net/http"
"strconv"
)
/**
* @api {get} /verify Verify User
* @apiDescription Used to verify that the user is authenticated. Optionally refreshing the token.
* @apiName VerifyUser
* @apiGroup Authentication
* @apiParam (Query String) {bool} refreshToken If the current user is still authenticated retrieve a new token with a refreshed expiration date. * Default=false
* @apiUse UserAuthHeader
* @apiUse UserDisplay
* @apiUse AuthHeaderResponse
* @apiPermission Authenticated
*/
func (ac *AuthController) verifyUser(c *gin.Context) {
// get logged in user
authUser, _ := api_utility.GetUserFromContext(c)
// check for refresh token
refreshTokenString := c.DefaultQuery("refreshToken", "false")
refreshToken, err := strconv.ParseBool(refreshTokenString)
if err != nil {
refreshToken = false
}
// if refresh requested, do it
if refreshToken {
// create token
tokenString, err := ac.createToken(authUser.Id)
if err != nil {
errors.ResponseWithSoftRedirect(c, http.StatusUnauthorized, "Error generating token.", REDIRECT_LOGIN)
return
}
c.Header("X-AUTH-TOKEN", tokenString)
}
c.JSON(http.StatusOK, authUser.GetUserDisplay())
}