-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add User implementation * Add login, logout, and update register routes * Update comment * Do an empty password check * Fix typo error * Add login and logout to auth router * Remove cookie-based auth in API * Have svelte put token into cookie for client
- Loading branch information
1 parent
a861ebc
commit 504bdef
Showing
13 changed files
with
394 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { Handle } from '@sveltejs/kit'; | ||
|
||
export const handle: Handle = async ({ event, resolve }) => { | ||
// Get the JWT from cookies | ||
const jwt = event.cookies.get('jwt'); | ||
// Set the user as authenticated in the locals object if JWT exists | ||
event.locals.authenticated = !!jwt; | ||
|
||
return await resolve(event); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { isAuthenticated } from './stores/auth'; | ||
import { goto } from '$app/navigation'; | ||
import { env } from '$env/dynamic/private'; | ||
|
||
interface AuthResponse { | ||
ok: boolean; | ||
error?: string; | ||
} | ||
|
||
export async function login(username: string, password: string): Promise<AuthResponse> { | ||
try { | ||
const response = await fetch(`${env.API_URL}/auth/login`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ username, password }) | ||
}); | ||
|
||
if (response.ok) { | ||
isAuthenticated.set(true); | ||
return { ok: true }; | ||
} | ||
|
||
return { | ||
ok: false, | ||
error: 'Invalid credentials' | ||
}; | ||
} catch (error) { | ||
console.error('Could not login', error); | ||
return { | ||
ok: false, | ||
error: 'An error occurred during login' | ||
}; | ||
} | ||
} | ||
|
||
export async function register( | ||
username: string, | ||
email: string, | ||
password: string, | ||
passwordConfirmation: string | ||
): Promise<AuthResponse> { | ||
try { | ||
const response = await fetch(`${env.API_URL}/auth/register`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
username, | ||
email, | ||
password, | ||
password_confirmation: passwordConfirmation | ||
}) | ||
}); | ||
|
||
if (response.ok) { | ||
isAuthenticated.set(true); | ||
return { ok: true }; | ||
} | ||
|
||
return { | ||
ok: false, | ||
error: 'Registration failed' | ||
}; | ||
} catch (error) { | ||
console.error('Could not register', error); | ||
return { | ||
ok: false, | ||
error: 'An error occurred during registration' | ||
}; | ||
} | ||
} | ||
|
||
export async function logout() { | ||
try { | ||
await fetch(`${env.API_URL}/auth/logout`, { | ||
method: 'POST' | ||
}); | ||
isAuthenticated.set(false); | ||
goto('/login'); | ||
} catch (error) { | ||
console.error('Logout failed:', error); | ||
} | ||
} |
Oops, something went wrong.