-
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.
- Loading branch information
1 parent
ef26a1d
commit b65ae3d
Showing
5 changed files
with
52 additions
and
7 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 was deleted.
Oops, something went wrong.
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,35 @@ | ||
import * as api from '../../lib/api'; | ||
import { fail, redirect } from '@sveltejs/kit'; | ||
import { MAX_COOKIE_AGE } from '../../lib/constants.ts'; | ||
|
||
export async function load({ locals }) { | ||
if (locals.user) { | ||
throw redirect(300, '/'); | ||
} | ||
} | ||
export const actions = { | ||
default: async ({ cookies, request }) => { | ||
const data = await request.formData(); | ||
const username = data.get('username')?.toString(); | ||
const password = data.get('password')?.toString(); | ||
|
||
if (!username || !password) { | ||
return fail(400, { username, password }); | ||
} | ||
const res = await api.login(username, password); | ||
const responseData = res; | ||
|
||
if (responseData.error) { | ||
const errorDetail = responseData.error.detail ? responseData.error.detail : 'Unknown error'; | ||
return fail(400, { message: errorDetail }); | ||
} else { | ||
if (responseData.data) { | ||
const value = btoa(JSON.stringify(responseData)); | ||
cookies.set('user', value, { path: '/', maxAge: MAX_COOKIE_AGE }); | ||
throw redirect(300, '/'); | ||
} else { | ||
return fail(400, { message: 'Response data is undefined' }); | ||
} | ||
} | ||
} | ||
}; |
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