feat: add admin session api
This commit is contained in:
35
server/resolvers/admin_session.go
Normal file
35
server/resolvers/admin_session.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package resolvers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/authorizerdev/authorizer/server/constants"
|
||||
"github.com/authorizerdev/authorizer/server/graph/model"
|
||||
"github.com/authorizerdev/authorizer/server/utils"
|
||||
)
|
||||
|
||||
func AdminSession(ctx context.Context) (*model.AdminLoginResponse, error) {
|
||||
gc, err := utils.GinContextFromContext(ctx)
|
||||
var res *model.AdminLoginResponse
|
||||
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
if !utils.IsSuperAdmin(gc) {
|
||||
return res, fmt.Errorf("unauthorized")
|
||||
}
|
||||
|
||||
hashedKey, err := utils.HashPassword(constants.EnvData.ADMIN_SECRET)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
utils.SetAdminCookie(gc, hashedKey)
|
||||
|
||||
res = &model.AdminLoginResponse{
|
||||
AccessToken: hashedKey,
|
||||
Message: "admin logged in successfully",
|
||||
}
|
||||
return res, nil
|
||||
}
|
Reference in New Issue
Block a user