Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions src/modules/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ export function createAuthModule(
/**
* Logout the current user
* Removes the token from localStorage and optionally redirects to a URL
* @param {string} [redirectUrl] - Optional URL to redirect to after logout
* @param redirectUrl - Optional URL to redirect to after logout
* @returns {Promise<void>}
*/
async logout(redirectUrl: string) {
async logout(redirectUrl?: string) {
// Remove token from axios headers
delete axios.defaults.headers.common["Authorization"];

Expand Down Expand Up @@ -106,6 +106,47 @@ export function createAuthModule(
}
},

/**
* Login via username and password
* @param email - User email
* @param password - User password
* @param turnstileToken - Optional Turnstile captcha token
* @returns Login response with access_token and user
*/
async loginViaUsernamePassword(
email: string,
password: string,
turnstileToken?: string
) {
try {
const response: { access_token: string; user: any } = await axios.post(
`/apps/${appId}/auth/login`,
{
email,
password,
...(turnstileToken && { turnstile_token: turnstileToken }),
}
);

const { access_token, user } = response;

if (access_token) {
this.setToken(access_token);
}

return {
access_token,
user,
};
} catch (error: any) {
// Handle authentication errors and cleanup
if (error.response?.status === 401) {
await this.logout();
}
throw error;
}
},

/**
* Verify if the current token is valid
* @returns {Promise<boolean>} True if token is valid
Expand Down
Loading