feat (encounter): create and checkout + checking soapi done
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package authentication
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
d "github.com/karincake/dodol"
|
||||
rw "github.com/karincake/risoles"
|
||||
|
||||
m "simrs-vx/internal/domain/main-entities/user"
|
||||
s "simrs-vx/internal/use-case/main-use-case/authentication"
|
||||
|
||||
pa "simrs-vx/pkg/auth-helper"
|
||||
)
|
||||
|
||||
func Login(w http.ResponseWriter, r *http.Request) {
|
||||
var input m.LoginDto
|
||||
if !(rw.ValidateStructByIOR(w, r.Body, &input)) {
|
||||
return
|
||||
}
|
||||
|
||||
// input.Position = Position
|
||||
res, err := s.GenToken(input)
|
||||
if err != nil {
|
||||
rw.WriteJSON(w, http.StatusUnauthorized, d.II{"errors": err}, nil)
|
||||
} else {
|
||||
rw.DataResponse(w, res, err)
|
||||
}
|
||||
}
|
||||
|
||||
func Logout(w http.ResponseWriter, r *http.Request) {
|
||||
ctxVal := r.Context().Value(pa.AuthKey{})
|
||||
if ctxVal == nil {
|
||||
rw.WriteJSON(w, http.StatusUnauthorized, d.IS{"message": "logout skiped. the request is done wihtout authorization."}, nil)
|
||||
return
|
||||
}
|
||||
authInfo := ctxVal.(*pa.AuthInfo)
|
||||
s.RevokeToken(authInfo.Uuid)
|
||||
rw.WriteJSON(w, http.StatusOK, d.IS{"message": "logged out"}, nil)
|
||||
}
|
||||
|
||||
func GuardMW(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
accessDetail, err := s.ExtractToken(r, s.AccessToken)
|
||||
if err != nil {
|
||||
rw.WriteJSON(w, http.StatusUnauthorized, err.(d.FieldError), nil)
|
||||
return
|
||||
}
|
||||
ctx := context.WithValue(r.Context(), pa.AuthKey{}, accessDetail)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user