Allow tokens to be null

This commit is contained in:
Melon 2024-05-18 01:27:54 +01:00
parent 3a7eef803e
commit c5a4d17a75
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
4 changed files with 25 additions and 23 deletions

View File

@ -5,9 +5,9 @@ CREATE TABLE users
email_verified BOOLEAN DEFAULT 0 NOT NULL, email_verified BOOLEAN DEFAULT 0 NOT NULL,
roles TEXT NOT NULL, roles TEXT NOT NULL,
userinfo TEXT NOT NULL, userinfo TEXT NOT NULL,
access_token TEXT NOT NULL, access_token TEXT,
refresh_token TEXT NOT NULL, refresh_token TEXT,
expiry DATETIME NOT NULL, expiry DATETIME,
updated_at DATETIME NOT NULL, updated_at DATETIME NOT NULL,
active BOOLEAN DEFAULT 1 NOT NULL active BOOLEAN DEFAULT 1 NOT NULL
); );

View File

@ -5,6 +5,7 @@
package database package database
import ( import (
"database/sql"
"time" "time"
) )
@ -26,9 +27,9 @@ type User struct {
EmailVerified bool `json:"email_verified"` EmailVerified bool `json:"email_verified"`
Roles string `json:"roles"` Roles string `json:"roles"`
Userinfo string `json:"userinfo"` Userinfo string `json:"userinfo"`
AccessToken string `json:"access_token"` AccessToken sql.NullString `json:"access_token"`
RefreshToken string `json:"refresh_token"` RefreshToken sql.NullString `json:"refresh_token"`
Expiry time.Time `json:"expiry"` Expiry sql.NullTime `json:"expiry"`
UpdatedAt time.Time `json:"updated_at"` UpdatedAt time.Time `json:"updated_at"`
Active bool `json:"active"` Active bool `json:"active"`
} }

View File

@ -7,6 +7,7 @@ package database
import ( import (
"context" "context"
"database/sql"
"time" "time"
) )
@ -97,9 +98,9 @@ LIMIT 1
` `
type GetUserTokenRow struct { type GetUserTokenRow struct {
AccessToken string `json:"access_token"` AccessToken sql.NullString `json:"access_token"`
RefreshToken string `json:"refresh_token"` RefreshToken sql.NullString `json:"refresh_token"`
Expiry time.Time `json:"expiry"` Expiry sql.NullTime `json:"expiry"`
} }
func (q *Queries) GetUserToken(ctx context.Context, subject string) (GetUserTokenRow, error) { func (q *Queries) GetUserToken(ctx context.Context, subject string) (GetUserTokenRow, error) {
@ -155,9 +156,9 @@ WHERE subject = ?
` `
type UpdateUserTokenParams struct { type UpdateUserTokenParams struct {
AccessToken string `json:"access_token"` AccessToken sql.NullString `json:"access_token"`
RefreshToken string `json:"refresh_token"` RefreshToken sql.NullString `json:"refresh_token"`
Expiry time.Time `json:"expiry"` Expiry sql.NullTime `json:"expiry"`
Subject string `json:"subject"` Subject string `json:"subject"`
} }

View File

@ -150,9 +150,9 @@ func (h *HttpServer) loginCallback(rw http.ResponseWriter, req *http.Request, _
if h.DbTx(rw, func(tx *database.Queries) error { if h.DbTx(rw, func(tx *database.Queries) error {
return tx.UpdateUserToken(req.Context(), database.UpdateUserTokenParams{ return tx.UpdateUserToken(req.Context(), database.UpdateUserTokenParams{
AccessToken: token.AccessToken, AccessToken: sql.NullString{String: token.AccessToken, Valid: true},
RefreshToken: token.RefreshToken, RefreshToken: sql.NullString{String: token.RefreshToken, Valid: true},
Expiry: token.Expiry, Expiry: sql.NullTime{Time: token.Expiry, Valid: true},
Subject: auth.Subject, Subject: auth.Subject,
}) })
}) { }) {