-
Notifications
You must be signed in to change notification settings - Fork 2
[Feature] Add JWT support #27
Changes from 17 commits
702d29c
a8a8b7f
e40dcd8
5c3903a
3293202
f066549
01fc78d
618cedb
93b0357
44b7c1d
4aeb97a
f3b8291
319feb0
ddf4d74
1069416
1bb16ed
9561299
7fec010
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package handler | ||
|
||
import ( | ||
"log" | ||
"tat_gogogo/domain/model" | ||
"tat_gogogo/domain/repository" | ||
"tat_gogogo/domain/service" | ||
"tat_gogogo/interface/controller" | ||
"tat_gogogo/usecase" | ||
|
||
jwt "github.com/appleboy/gin-jwt/v2" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
/* | ||
CoursesHandler is a function for gin to handle courses api | ||
*/ | ||
func CoursesHandler(c *gin.Context) { | ||
targetStudentID := c.Query("targetStudentID") | ||
year := c.Query("year") | ||
semester := c.Query("semester") | ||
|
||
claims := jwt.ExtractClaims(c) | ||
studentID := claims["studentID"].(string) | ||
password := claims["password"].(string) | ||
|
||
loginController := controller.NewLoginController(studentID, password) | ||
courseController := controller.NewCoursesController(studentID, password, targetStudentID, year, semester) | ||
|
||
result, err := loginController.Login() | ||
if err != nil { | ||
c.Status(500) | ||
return | ||
} | ||
|
||
if !result.GetSuccess() { | ||
c.JSON(401, gin.H{ | ||
"message": result.GetData(), | ||
}) | ||
return | ||
} | ||
|
||
isLoginCurriculumSuccess, err := loginController.LoginCurriculum() | ||
if err != nil { | ||
c.Status(500) | ||
return | ||
} | ||
|
||
if !isLoginCurriculumSuccess { | ||
c.JSON(401, gin.H{ | ||
"message": "登入課程系統失敗", | ||
}) | ||
return | ||
} | ||
|
||
curriculums, err := courseController.GetCurriculums() | ||
if err != nil { | ||
c.Status(500) | ||
return | ||
} | ||
|
||
isSameYearAndSem := courseController.IsSameYearAndSem(curriculums) | ||
|
||
if !isSameYearAndSem { | ||
result := getNoDataResult() | ||
c.JSON(result.GetStatus(), gin.H{ | ||
"message": result.GetData(), | ||
}) | ||
return | ||
} | ||
|
||
infoResult, err := courseController.GetInfoResult() | ||
if err != nil { | ||
log.Panicln(err) | ||
c.Status(500) | ||
return | ||
} | ||
|
||
c.JSON(infoResult.GetStatus(), infoResult.GetData()) | ||
|
||
} | ||
|
||
func getNoDataResult() *model.Result { | ||
resultRepo := repository.NewResultRepository() | ||
resultService := service.NewResultService(resultRepo) | ||
resultUsecase := usecase.NewResultUseCase(resultRepo, resultService) | ||
return resultUsecase.GetNoDataResult() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package handler | ||
|
||
import ( | ||
"tat_gogogo/interface/controller" | ||
|
||
jwt "github.com/appleboy/gin-jwt/v2" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
/* | ||
CurriculumHandler is a function for gin to handle curriculum api | ||
*/ | ||
func CurriculumHandler(c *gin.Context) { | ||
targetStudentID := c.Query("targetStudentID") | ||
|
||
claims := jwt.ExtractClaims(c) | ||
studentID := claims["studentID"].(string) | ||
password := claims["password"].(string) | ||
|
||
loginController := controller.NewLoginController(studentID, password) | ||
curriculumController := controller.NewCurriculumController(studentID, password, targetStudentID) | ||
|
||
result, err := loginController.Login() | ||
if err != nil { | ||
c.Status(500) | ||
return | ||
} | ||
|
||
if !result.GetSuccess() { | ||
c.JSON(401, gin.H{ | ||
"message": result.GetData(), | ||
}) | ||
return | ||
} | ||
|
||
isLoginCurriculumSuccess, err := loginController.LoginCurriculum() | ||
if err != nil { | ||
c.Status(500) | ||
return | ||
} | ||
|
||
if !isLoginCurriculumSuccess { | ||
c.JSON(401, gin.H{ | ||
"message": "登入課程系統失敗", | ||
}) | ||
return | ||
} | ||
|
||
curriculumResult, err := curriculumController.GetCurriculumResult() | ||
if err != nil { | ||
c.Status(500) | ||
return | ||
} | ||
|
||
c.JSON(curriculumResult.GetStatus(), curriculumResult.GetData()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package handler | ||
|
||
import ( | ||
"log" | ||
|
||
"tat_gogogo/infrastructure/middleware" | ||
"tat_gogogo/interface/controller" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
/* | ||
LoginHandler is a function for gin to handle login api | ||
*/ | ||
func LoginHandler(c *gin.Context) { | ||
authMiddleware, err := middleware.NewAuthMiddleware() | ||
if err != nil { | ||
c.Status(500) | ||
log.Fatal("JWT Error:" + err.Error()) | ||
|
||
} | ||
|
||
studentID := c.PostForm("studentID") | ||
password := c.PostForm("password") | ||
|
||
loginController := controller.NewLoginController(studentID, password) | ||
result, err := loginController.Login() | ||
|
||
if err != nil { | ||
log.Panicln("failed to fetch login cookie") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as here, do I just use the regular print? Is there any rule that I have to fellow. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. Pretty much the same as the previous comment. |
||
c.Status(500) | ||
return | ||
} | ||
|
||
if result.GetStatus() != 200 { | ||
c.JSON(result.GetStatus(), gin.H{ | ||
"message": result.GetData(), | ||
}) | ||
return | ||
} | ||
|
||
authMiddleware.LoginHandler(c) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log.Fatal
will crash your code. According to their documentation, it willlog.Print
then it willos.Exit(1)
. You shouldn't crash your code in the case of a failed Middleware AuthenticationThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so in here I just use the regular print?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your common
log.Printf
will work, and whenever you purposefully want to break the execution of your code, usepanic
oros.Exit(1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've fix it with regular print