silvermobi/handler/authentication.go

64 lines
1.4 KiB
Go
Raw Permalink Normal View History

2023-08-08 10:28:43 +00:00
package handler
import (
"context"
"time"
2024-10-31 19:32:54 +00:00
"git.coopgo.io/coopgo-apps/silvermobi/models"
2023-08-08 10:28:43 +00:00
)
2024-10-31 19:32:54 +00:00
func (h *SilverMobiHandler) Login(ctx context.Context, username string, password string) (jwt string, err error) {
var (
account *models.Account
pTTL time.Duration
)
if account, err = h.Services.MobilityAccounts.Login(ctx, username, password, "silvermobi"); err != nil {
2023-08-08 10:28:43 +00:00
return
}
2024-10-31 19:32:54 +00:00
2023-08-08 10:28:43 +00:00
key := h.Config.GetString("identification.local.jwt_secret")
ttl := h.Config.GetString("identification.local.ttl")
2024-10-31 19:32:54 +00:00
if pTTL, err = time.ParseDuration(ttl); err != nil {
2023-08-08 10:28:43 +00:00
return "", err
}
2024-10-31 19:32:54 +00:00
return account.CreateToken(pTTL, key)
2023-08-08 10:28:43 +00:00
}
2024-10-31 19:32:54 +00:00
func (h *SilverMobiHandler) Register(ctx context.Context, username string, password string, email string,
phoneNumber string, firstName string, lastName string) (jwt string, err error) {
2024-10-31 19:32:54 +00:00
var (
account *models.Account
pTTL time.Duration
)
if account, err = h.Services.MobilityAccounts.Register(
2023-08-08 10:28:43 +00:00
ctx,
username,
password,
email,
phoneNumber,
2023-08-08 10:28:43 +00:00
map[string]any{
"first_name": firstName,
"last_name": lastName,
2023-08-08 10:28:43 +00:00
"email": email,
"phone_number": phoneNumber,
2023-08-08 10:28:43 +00:00
},
"silvermobi",
2024-10-31 19:32:54 +00:00
); err != nil {
2023-08-08 10:28:43 +00:00
return "", err
}
key := h.Config.GetString("identification.local.jwt_secret")
ttl := h.Config.GetString("identification.local.ttl")
2024-10-31 19:32:54 +00:00
if pTTL, err = time.ParseDuration(ttl); err != nil {
2023-08-08 10:28:43 +00:00
return "", err
}
2024-10-31 19:32:54 +00:00
return account.CreateToken(pTTL, key)
2023-08-08 10:28:43 +00:00
}