Skip to content

Commit

Permalink
Add error detalization
Browse files Browse the repository at this point in the history
  • Loading branch information
berejant committed Nov 2, 2023
1 parent 0cefd5c commit d22ebd7
Show file tree
Hide file tree
Showing 2 changed files with 596 additions and 333 deletions.
62 changes: 37 additions & 25 deletions ApiController.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"embed"
"encoding/json"
"errors"
"fmt"
"github.com/berejant/go-kneu"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
Expand Down Expand Up @@ -118,43 +119,50 @@ func (controller *ApiController) completeAuth(c *gin.Context) {
}

authOptionsClaims, err := controller.parseState(state)
if err == nil {
tokenResponse, err = controller.oauthClient.GetOauthToken(controller.oauthRedirectUrl, code)
if err != nil {
fmt.Fprintf(controller.out, "Failed to parse state: %s\n", err.Error())
controller.errorResponse(c, "Невірний стан")
return
}

tokenResponse, err = controller.oauthClient.GetOauthToken(controller.oauthRedirectUrl, code)
if err != nil {
fmt.Fprintf(controller.out, "Failed to get token: %s\n", err.Error())
controller.errorResponse(c, "Помилка отримання токена")
return
}

if err == nil && tokenResponse.UserId == adminUserid {
authOptionsClaims.KneuUserId = tokenResponse.UserId

state, err = controller.buildState(authOptionsClaims)
if err == nil {
controller.responseWithAdminAuthFrom(c, state)
return
}
controller.responseWithAdminAuthFrom(c, state)
return
}

if err == nil {
userMeResponse, err = controller.apiClientFactory(tokenResponse.AccessToken).GetUserMe()
userMeResponse, err = controller.apiClientFactory(tokenResponse.AccessToken).GetUserMe()
if err != nil {
fmt.Fprintf(controller.out, "Failed to get user me: %s\n", err.Error())
controller.errorResponse(c, "Помилка отримання даних користувача")
return
}

if err == nil && userMeResponse.Type != "student" {
c.HTML(http.StatusBadRequest, "error.html", gin.H{
"error": html.EscapeString("Вам потрібно використати особистий кабінет студента"),
})
if userMeResponse.Type != "student" {
controller.errorResponse(c, "Вам потрібно використати особистий кабінет студента")
return
}

if err == nil {
err = controller.finishAuthorization(authOptionsClaims, Student{
Id: userMeResponse.StudentId,
LastName: userMeResponse.LastName,
FirstName: userMeResponse.FirstName,
MiddleName: userMeResponse.MiddleName,
Gender: events.GenderFromString(userMeResponse.Gender),
})
}
err = controller.finishAuthorization(authOptionsClaims, Student{
Id: userMeResponse.StudentId,
LastName: userMeResponse.LastName,
FirstName: userMeResponse.FirstName,
MiddleName: userMeResponse.MiddleName,
Gender: events.GenderFromString(userMeResponse.Gender),
})

if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{})
fmt.Fprintf(controller.out, "Failed to auth user: %s\n", err.Error())
controller.errorResponse(c, "Помилка завершення авторизації")
return
} else {
controller.successRedirect(c, authOptionsClaims)
Expand Down Expand Up @@ -210,9 +218,7 @@ func (controller *ApiController) completeAdminAuth(c *gin.Context) {
}
}

c.HTML(http.StatusBadRequest, "error.html", gin.H{
"error": html.EscapeString(err.Error()),
})
controller.errorResponse(c, err.Error())
}

func (controller *ApiController) finishAuthorization(claims AuthOptionsClaims, student Student) error {
Expand Down Expand Up @@ -254,3 +260,9 @@ func (controller *ApiController) parseState(state string) (claims AuthOptionsCla

return claims, err
}

func (controller *ApiController) errorResponse(c *gin.Context, errMessage string) {
c.HTML(http.StatusBadRequest, "error.html", gin.H{
"error": html.EscapeString(errMessage),
})
}
Loading

0 comments on commit d22ebd7

Please sign in to comment.