@@ -42,8 +42,58 @@ export const useAuthStore = create<AuthStore>()(
42
42
set ( { hydrated : true } )
43
43
} ,
44
44
45
-
45
+ async verifySession ( ) {
46
+ try {
47
+ const session = await account . getSession ( "current" ) ;
48
+ set ( { session} ) ;
49
+ } catch ( error ) {
50
+ console . log ( "Error verifying session" , error ) ;
51
+ }
52
+ } ,
53
+
54
+ async login ( email , password ) {
55
+ try {
56
+ const session = await account . createEmailPasswordSession ( email , password ) ;
57
+ const [ user , { jwt} ] = await Promise . all ( [
58
+ account . get < UserPrefs > ( ) ,
59
+ account . createJWT ( )
60
+ ] )
61
+ if ( ! user . prefs ?. reputation ) await account . updatePrefs < UserPrefs > ( { reputation : 0 } ) ;
62
+ set ( { session, user, jwt} ) ;
63
+ return { success : true , } ;
64
+ } catch ( error ) {
65
+ console . log ( "Error logging in" , error ) ;
66
+ return {
67
+ success : false ,
68
+ error : error instanceof AppwriteException ? error : null
69
+ } ;
70
+ }
71
+ } ,
72
+
73
+ async createAccount ( name :string , email :string , password :string ) {
74
+ try {
75
+ await account . create ( ID . unique ( ) , name , email , password ) ;
76
+ return { success : true } ;
77
+ } catch ( error ) {
78
+ console . log ( "Error creating account" , error ) ;
79
+ return {
80
+ success : false ,
81
+ error : error instanceof AppwriteException ? error : null
82
+ } ;
83
+ }
84
+ } ,
85
+
86
+ async logout ( ) {
87
+ try {
88
+ await account . deleteSessions ( ) ;
89
+ set ( { session : null , jwt :null , user : null } ) ;
90
+ } catch ( error ) {
91
+ console . log ( "Error logout" , error ) ;
92
+ }
93
+ } ,
46
94
} ) ) ,
95
+
96
+ //for immer to work we need to use the immer middleware
47
97
{
48
98
name : "auth" ,
49
99
onRehydrateStorage ( ) {
0 commit comments