Skip to content

Commit

Permalink
Fixed cookies domain not being set correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
rezk2ll committed Oct 30, 2023
1 parent 500fa5c commit a8d1345
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/routes/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '$lib/services/user';
import { validateName, validateNickName } from '$lib/utils/username';
import authService from '$lib/services/auth';
import { extractMainDomain } from '$lib/utils/url';

export const load: PageServerLoad = async ({ parent }) => {
await Client.getClient();
Expand Down Expand Up @@ -146,15 +147,17 @@ export const actions: Actions = {

const authSessionCookie = await authService.login(nickname, password);

cookies.set(authService.cookieName, authSessionCookie, { domain: url.host });
cookies.set(authService.cookieName, authSessionCookie, {
domain: extractMainDomain(url.host)
});

throw redirect(302, '/success');
} catch (err) {
if ((err as Redirect).location) {
throw err;
}

console.error({ err })
console.error({ err });

return fail(500, { error: true });
}
Expand Down Expand Up @@ -187,15 +190,15 @@ export const actions: Actions = {
user: user?.cn
}));

cookies.set(authService.cookieName, cookie, { domain: url.host });
cookies.set(authService.cookieName, cookie, { domain: extractMainDomain(url.host) });

throw redirect(302, '/success');
} catch (err) {
if ((err as Redirect).location) {
throw err;
}

console.error({ err })
console.error({ err });

return fail(500, { failed_login: true });
}
Expand Down
3 changes: 2 additions & 1 deletion src/routes/api/register/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { validatePassword } from '$lib/utils/password';
import { checkNickNameAvailability, checkPhoneAvailability, signup } from '$lib/services/user';
import authService from '$lib/services/auth';
import { validateName, validateNickName } from '$lib/utils/username';
import { extractMainDomain } from '$lib/utils/url';

export const POST: RequestHandler = async ({ request, locals, cookies, url }) => {
const { nickname, phone, firstname, lastname, password } = await request.json();
Expand Down Expand Up @@ -53,7 +54,7 @@ export const POST: RequestHandler = async ({ request, locals, cookies, url }) =>

const authSessionCookie = await authService.login(nickname, password);

cookies.set(authService.cookieName, authSessionCookie, { domain: url.host });
cookies.set(authService.cookieName, authSessionCookie, { domain: extractMainDomain(url.host) });

return new Response('ok', { status: 201 });
};

0 comments on commit a8d1345

Please sign in to comment.