11import { UrBackendClient } from '../client' ;
2- import { AuthUser , AuthResponse , SignUpPayload , LoginPayload } from '../types' ;
2+ import {
3+ AuthUser ,
4+ AuthResponse ,
5+ SignUpPayload ,
6+ LoginPayload ,
7+ UpdateProfilePayload ,
8+ ChangePasswordPayload ,
9+ VerifyEmailPayload ,
10+ ResendOtpPayload ,
11+ RequestPasswordResetPayload ,
12+ ResetPasswordPayload ,
13+ SocialExchangePayload ,
14+ SocialExchangeResponse ,
15+ RequestOptions ,
16+ } from '../types' ;
317import { AuthError } from '../errors' ;
418
519export class AuthModule {
@@ -21,7 +35,15 @@ export class AuthModule {
2135 const response = await this . client . request < AuthResponse > ( 'POST' , '/api/userAuth/login' , {
2236 body : payload ,
2337 } ) ;
24- this . sessionToken = response . token ;
38+
39+ this . sessionToken = response . accessToken || response . token ;
40+
41+ if ( ! response . accessToken && response . token ) {
42+ console . warn (
43+ 'urbackend-sdk: The server returned "token" which is deprecated. Please update your backend to return "accessToken".' ,
44+ ) ;
45+ }
46+
2547 return response ;
2648 }
2749
@@ -32,16 +54,156 @@ export class AuthModule {
3254 const activeToken = token || this . sessionToken ;
3355
3456 if ( ! activeToken ) {
35- throw new AuthError ( 'Authentication token is required for /me endpoint' , 401 , '/api/userAuth/me' ) ;
57+ throw new AuthError (
58+ 'Authentication token is required for /me endpoint' ,
59+ 401 ,
60+ '/api/userAuth/me' ,
61+ ) ;
3662 }
3763
3864 return this . client . request < AuthUser > ( 'GET' , '/api/userAuth/me' , { token : activeToken } ) ;
3965 }
4066
4167 /**
42- * Clear the local session token
68+ * Update the current authenticated user's profile
69+ */
70+ public async updateProfile ( payload : UpdateProfilePayload , token ?: string ) : Promise < { message : string } > {
71+ const activeToken = token || this . sessionToken ;
72+ if ( ! activeToken ) {
73+ throw new AuthError ( 'Authentication token is required to update profile' , 401 , '/api/userAuth/update-profile' ) ;
74+ }
75+ return this . client . request < { message : string } > ( 'PUT' , '/api/userAuth/update-profile' , {
76+ body : payload ,
77+ token : activeToken ,
78+ } ) ;
79+ }
80+
81+ /**
82+ * Change the current authenticated user's password
83+ */
84+ public async changePassword ( payload : ChangePasswordPayload , token ?: string ) : Promise < { message : string } > {
85+ const activeToken = token || this . sessionToken ;
86+ if ( ! activeToken ) {
87+ throw new AuthError ( 'Authentication token is required to change password' , 401 , '/api/userAuth/change-password' ) ;
88+ }
89+ return this . client . request < { message : string } > ( 'PUT' , '/api/userAuth/change-password' , {
90+ body : payload ,
91+ token : activeToken ,
92+ } ) ;
93+ }
94+
95+ /**
96+ * Verify user email with OTP
97+ */
98+ public async verifyEmail ( payload : VerifyEmailPayload ) : Promise < { message : string } > {
99+ return this . client . request < { message : string } > ( 'POST' , '/api/userAuth/verify-email' , {
100+ body : payload ,
101+ } ) ;
102+ }
103+
104+ /**
105+ * Resend verification OTP
106+ */
107+ public async resendVerificationOtp ( payload : ResendOtpPayload ) : Promise < { message : string } > {
108+ return this . client . request < { message : string } > ( 'POST' , '/api/userAuth/resend-verification-otp' , {
109+ body : payload ,
110+ } ) ;
111+ }
112+
113+ /**
114+ * Request password reset OTP
115+ */
116+ public async requestPasswordReset ( payload : RequestPasswordResetPayload ) : Promise < { message : string } > {
117+ return this . client . request < { message : string } > ( 'POST' , '/api/userAuth/request-password-reset' , {
118+ body : payload ,
119+ } ) ;
120+ }
121+
122+ /**
123+ * Reset user password with OTP
43124 */
44- public logout ( ) : void {
125+ public async resetPassword ( payload : ResetPasswordPayload ) : Promise < { message : string } > {
126+ return this . client . request < { message : string } > ( 'POST' , '/api/userAuth/reset-password' , {
127+ body : payload ,
128+ } ) ;
129+ }
130+
131+ /**
132+ * Get public-safe profile by username
133+ */
134+ public async publicProfile ( username : string ) : Promise < AuthUser > {
135+ return this . client . request < AuthUser > ( 'GET' , `/api/userAuth/public/${ username } ` ) ;
136+ }
137+
138+ /**
139+ * Refresh the access token
140+ * @param refreshToken Optional refresh token for header mode. If omitted, uses cookie mode.
141+ */
142+ public async refreshToken ( refreshToken ?: string ) : Promise < AuthResponse > {
143+ const options : RequestOptions = { } ;
144+ if ( refreshToken ) {
145+ options . headers = { 'x-refresh-token' : refreshToken , 'x-refresh-token-mode' : 'header' } ;
146+ } else {
147+ options . credentials = 'include' ;
148+ }
149+
150+ const response = await this . client . request < AuthResponse > ( 'POST' , '/api/userAuth/refresh-token' , options ) ;
151+ this . sessionToken = response . accessToken || response . token ;
152+ return response ;
153+ }
154+
155+ /**
156+ * Returns the start URL for social authentication.
157+ * Redirect the user's browser to this URL to begin the flow.
158+ */
159+ public socialStart ( provider : 'github' | 'google' ) : string {
160+ return `${ this . client . getBaseUrl ( ) } /api/userAuth/social/${ provider } /start?key=${ this . client . getApiKey ( ) } ` ;
161+ }
162+
163+ /**
164+ * Exchange social auth rtCode for a refresh token
165+ */
166+ public async socialExchange ( payload : SocialExchangePayload ) : Promise < SocialExchangeResponse > {
167+ return this . client . request < SocialExchangeResponse > ( 'POST' , '/api/userAuth/social/exchange' , {
168+ body : payload ,
169+ } ) ;
170+ }
171+
172+ /**
173+ * Revoke the current session and clear local state
174+ */
175+ public async logout ( token ?: string ) : Promise < { success : boolean ; message : string } > {
176+ const activeToken = token || this . sessionToken ;
177+ let result = { success : true , message : 'Logged out locally' } ;
178+
179+ if ( activeToken ) {
180+ try {
181+ result = await this . client . request < { success : boolean ; message : string } > (
182+ 'POST' ,
183+ '/api/userAuth/logout' ,
184+ { token : activeToken , credentials : 'include' } ,
185+ ) ;
186+ } catch ( e ) {
187+ // Silently fail if server logout fails, we still want to clear local state
188+ console . warn ( 'urbackend-sdk: Server logout failed' , e ) ;
189+ }
190+ }
191+
45192 this . sessionToken = undefined ;
193+ return result ;
194+ }
195+
196+ /**
197+ * Manually set the session token (e.g. after social auth exchange)
198+ */
199+ public setToken ( token : string ) : void {
200+ this . sessionToken = token ;
201+ }
202+
203+ /**
204+ * Get the current stored session token
205+ */
206+ public getToken ( ) : string | undefined {
207+ return this . sessionToken ;
46208 }
47209}
0 commit comments