mirror of
https://github.com/Theodor-Springmann-Stiftung/musenalm.git
synced 2025-10-29 09:15:33 +00:00
Registration form for new users
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
type FixedAccessToken struct {
|
||||
Token string `json:"token"`
|
||||
CSRF string `json:"csrf"`
|
||||
User string `json:"user"`
|
||||
Created string `json:"created"`
|
||||
Updated string `json:"updated"`
|
||||
@@ -15,6 +16,10 @@ type FixedAccessToken struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func (s *FixedAccessToken) IsExpired() bool {
|
||||
return s.Expires.IsZero() || s.Expires.Before(types.NowDateTime())
|
||||
}
|
||||
|
||||
var _ core.RecordProxy = (*AccessToken)(nil)
|
||||
|
||||
type AccessToken struct {
|
||||
@@ -43,6 +48,14 @@ func (u *AccessToken) User() string {
|
||||
return u.GetString(ACCESS_TOKENS_USER_FIELD)
|
||||
}
|
||||
|
||||
func (u *AccessToken) CSRF() string {
|
||||
return u.GetString(ACCESS_TOKENS_CSRF_FIELD)
|
||||
}
|
||||
|
||||
func (u *AccessToken) SetCSRF(csrf string) {
|
||||
u.Set(ACCESS_TOKENS_CSRF_FIELD, csrf)
|
||||
}
|
||||
|
||||
func (u *AccessToken) SetUser(userId string) {
|
||||
u.Set(ACCESS_TOKENS_USER_FIELD, userId)
|
||||
}
|
||||
@@ -79,6 +92,10 @@ func (u *AccessToken) SetStatus(status string) {
|
||||
u.Set(ACCESS_TOKENS_STATUS_FIELD, status)
|
||||
}
|
||||
|
||||
func (u *AccessToken) IsExpired() bool {
|
||||
return u.Expires().IsZero() || u.Expires().Before(types.NowDateTime())
|
||||
}
|
||||
|
||||
func (u *AccessToken) Fixed() *FixedAccessToken {
|
||||
return &FixedAccessToken{
|
||||
Token: u.Token(),
|
||||
|
||||
47
dbmodels/access_tokens_functions.go
Normal file
47
dbmodels/access_tokens_functions.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package dbmodels
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/types"
|
||||
)
|
||||
|
||||
func CreateAccessToken(
|
||||
app core.App,
|
||||
userID string,
|
||||
url string,
|
||||
duration time.Duration,
|
||||
) (*AccessToken, error) {
|
||||
collection, err := app.FindCollectionByNameOrId(ACCESS_TOKENS_TABLE)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to find '%s' collection: %w", ACCESS_TOKENS_TABLE, err)
|
||||
}
|
||||
|
||||
accesTokenClear, err := generateSecureRandomToken(secureTokenByteLength)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to generate session token: %w", err)
|
||||
}
|
||||
|
||||
csrfTokenClear, err := generateSecureRandomToken(secureTokenByteLength)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to generate CSRF token: %w", err)
|
||||
}
|
||||
|
||||
record := core.NewRecord(collection)
|
||||
token := NewAccessToken(record)
|
||||
|
||||
token.SetToken(accesTokenClear)
|
||||
token.SetCSRF(csrfTokenClear)
|
||||
token.SetUser(userID)
|
||||
token.SetURL(url)
|
||||
token.SetStatus(TOKEN_STATUS_VALUES[0]) // Active
|
||||
token.SetExpires(types.NowDateTime().Add(duration))
|
||||
|
||||
if err := app.Save(record); err != nil {
|
||||
return nil, fmt.Errorf("failed to save access token record: %w", err)
|
||||
}
|
||||
|
||||
return token, nil
|
||||
}
|
||||
35
dbmodels/user_functions.go
Normal file
35
dbmodels/user_functions.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package dbmodels
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
)
|
||||
|
||||
func CreateUser(
|
||||
app core.App,
|
||||
email string,
|
||||
password string,
|
||||
name string,
|
||||
role string,
|
||||
) (*User, error) {
|
||||
collection, err := app.FindCollectionByNameOrId(USERS_TABLE)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to find '%s' collection: %w", USERS_TABLE, err)
|
||||
}
|
||||
|
||||
record := core.NewRecord(collection)
|
||||
user := NewUser(record)
|
||||
user.SetEmail(email)
|
||||
user.SetPassword(password)
|
||||
user.SetName(name)
|
||||
user.SetVerified(true)
|
||||
user.SetDeactivated(false)
|
||||
user.SetRole(role)
|
||||
|
||||
if err := app.Save(record); err != nil {
|
||||
return nil, fmt.Errorf("failed to save user record: %w", err)
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
Reference in New Issue
Block a user