File tree Expand file tree Collapse file tree 4 files changed +43
-10
lines changed Expand file tree Collapse file tree 4 files changed +43
-10
lines changed Original file line number Diff line number Diff line change 55
66 "github.com/authorizerdev/authorizer/server/constants"
77 "github.com/authorizerdev/authorizer/server/enum"
8+ "github.com/google/uuid"
89 "gorm.io/driver/mysql"
910 "gorm.io/driver/postgres"
1011 "gorm.io/driver/sqlite"
@@ -18,7 +19,7 @@ type Manager interface {
1819 GetUsers () ([]User , error )
1920 GetUserByEmail (email string ) (User , error )
2021 GetUserByID (email string ) (User , error )
21- UpdateVerificationTime (verifiedAt int64 , id uint ) error
22+ UpdateVerificationTime (verifiedAt int64 , id uuid. UUID ) error
2223 AddVerification (verification VerificationRequest ) (VerificationRequest , error )
2324 GetVerificationByToken (token string ) (VerificationRequest , error )
2425 DeleteToken (email string ) error
Original file line number Diff line number Diff line change 11package db
22
3- import "log"
3+ import (
4+ "log"
5+
6+ "github.com/google/uuid"
7+ "gorm.io/gorm"
8+ "gorm.io/gorm/clause"
9+ )
410
511type Role struct {
6- ID uint `gorm:"primaryKey"`
7- Role string
12+ ID uuid.UUID `gorm:"type:uuid;"`
13+ Role string `gorm:"unique"`
14+ }
15+
16+ func (r * Role ) BeforeCreate (tx * gorm.DB ) (err error ) {
17+ r .ID = uuid .New ()
18+
19+ return
820}
921
1022// SaveRoles function to save roles
1123func (mgr * manager ) SaveRoles (roles []Role ) error {
12- res := mgr .db .Create (& roles )
24+ res := mgr .db .Clauses (
25+ clause.OnConflict {
26+ OnConstraint : "authorizer_roles_role_key" ,
27+ DoNothing : true ,
28+ }).Create (& roles )
1329 if res .Error != nil {
1430 log .Println (`Error saving roles` )
1531 return res .Error
Original file line number Diff line number Diff line change @@ -4,11 +4,13 @@ import (
44 "log"
55 "time"
66
7+ "github.com/google/uuid"
8+ "gorm.io/gorm"
79 "gorm.io/gorm/clause"
810)
911
1012type User struct {
11- ID uint `gorm:"primaryKey "`
13+ ID uuid. UUID `gorm:"type:uuid; "`
1214 FirstName string
1315 LastName string
1416 Email string `gorm:"unique"`
@@ -21,6 +23,12 @@ type User struct {
2123 Roles string
2224}
2325
26+ func (u * User ) BeforeCreate (tx * gorm.DB ) (err error ) {
27+ u .ID = uuid .New ()
28+
29+ return
30+ }
31+
2432// SaveUser function to add user even with email conflict
2533func (mgr * manager ) SaveUser (user User ) (User , error ) {
2634 result := mgr .db .Clauses (
@@ -42,7 +50,7 @@ func (mgr *manager) UpdateUser(user User) (User, error) {
4250 result := mgr .db .Clauses (
4351 clause.OnConflict {
4452 UpdateAll : true ,
45- Columns : []clause.Column {{Name : "id " }},
53+ Columns : []clause.Column {{Name : "email " }},
4654 }).Create (& user )
4755
4856 if result .Error != nil {
@@ -85,7 +93,7 @@ func (mgr *manager) GetUserByID(id string) (User, error) {
8593 return user , nil
8694}
8795
88- func (mgr * manager ) UpdateVerificationTime (verifiedAt int64 , id uint ) error {
96+ func (mgr * manager ) UpdateVerificationTime (verifiedAt int64 , id uuid. UUID ) error {
8997 user := & User {
9098 ID : id ,
9199 }
Original file line number Diff line number Diff line change @@ -3,19 +3,27 @@ package db
33import (
44 "log"
55
6+ "github.com/google/uuid"
7+ "gorm.io/gorm"
68 "gorm.io/gorm/clause"
79)
810
911type VerificationRequest struct {
10- ID uint `gorm:"primaryKey "`
11- Token string `gorm:"index"`
12+ ID uuid. UUID `gorm:"type:uuid; "`
13+ Token string `gorm:"index"`
1214 Identifier string
1315 ExpiresAt int64
1416 CreatedAt int64 `gorm:"autoCreateTime"`
1517 UpdatedAt int64 `gorm:"autoUpdateTime"`
1618 Email string `gorm:"unique"`
1719}
1820
21+ func (v * VerificationRequest ) BeforeCreate (tx * gorm.DB ) (err error ) {
22+ v .ID = uuid .New ()
23+
24+ return
25+ }
26+
1927// AddVerification function to add verification record
2028func (mgr * manager ) AddVerification (verification VerificationRequest ) (VerificationRequest , error ) {
2129 result := mgr .db .Clauses (clause.OnConflict {
You can’t perform that action at this time.
0 commit comments