Skip to content

Commit

Permalink
fix: signin with page.ts, signOut fix in layout
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlking committed Jun 23, 2024
1 parent f7a77c2 commit adc0c25
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 40 deletions.
3 changes: 2 additions & 1 deletion apps/console/src/lib/server/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const log = new Logger('server:middleware:auth');
*/
export const auth = (async ({ event, resolve }) => {
const { url, cookies } = event;
log.debug('auth: pathname:', url.pathname);
log.debug('pathname:', url.pathname);

const sessionCookieValue = cookies.get(NHOST_SESSION_KEY);
const initialSession = sessionCookieValue ? (JSON.parse(atob(sessionCookieValue)) as NhostSession) : undefined;
Expand All @@ -32,6 +32,7 @@ export const auth = (async ({ event, resolve }) => {
const currentTime = Math.floor(Date.now() / 1000);
const tokenExpirationTime = nhost.auth.getDecodedAccessToken()?.exp;
const accessTokenExpired = session && tokenExpirationTime && currentTime > tokenExpirationTime;
log.debug({ accessTokenExpired, refreshToken });

if (accessTokenExpired || refreshToken) {
log.debug('in accessTokenExpired || refreshToken');
Expand Down
2 changes: 2 additions & 0 deletions apps/console/src/routes/(auth)/signin/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { zod } from 'sveltekit-superforms/adapters';

const log = new Logger('server:auth:signin');

/*
export const load = async (event) => {
const {
locals: { nhost },
Expand All @@ -27,6 +28,7 @@ export const load = async (event) => {
const pwlForm = await superValidate(zod(pwlSchema));
return { pwForm, pwlForm };
};
*/

export const actions = {
password: async (event) => {
Expand Down
14 changes: 14 additions & 0 deletions apps/console/src/routes/(auth)/signin/+page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { i18n } from '$lib/i18n';
import { pwSchema, pwlSchema } from '$lib/schema/user';
import { isAuthenticated } from '$lib/stores/user';
import { redirect } from '@sveltejs/kit';
import { get } from 'svelte/store';
import { superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';

export const load = async (event) => {
if (get(isAuthenticated)) redirect(302, i18n.resolveRoute('/dashboard'));
const pwForm = await superValidate(zod(pwSchema));
const pwlForm = await superValidate(zod(pwlSchema));
return { pwForm, pwlForm };
};
38 changes: 0 additions & 38 deletions apps/console/src/routes/(auth)/signup/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,6 @@ import { zod } from 'sveltekit-superforms/adapters';

const log = new Logger('server:auth:signup');

/*
const searchOrganizationsStore = new SearchOrganizationsStore();
export const load = async (event) => {
const {
locals: { nhost },
} = event;
// Preflight prevents direct posting.
// If preflight option is true and this function isn't called
// before posting, request will be limited:
await limiter.cookieLimiter?.preflight(event);
const isAuthenticated = nhost.auth.isAuthenticated();
// log.debug({isAuthenticated});
if (isAuthenticated) redirectWithFlash(302, i18n.resolveRoute('/dashboard'));
const form = await superValidate(zod(signUpSchema));
// fetch orgs and render errors if backend throw error.
const { errors, data } = await searchOrganizationsStore.fetch({
event,
blocking: true,
metadata: { logResult: true, useRole: 'public' },
variables: {},
});
if (errors) {
for (const error of errors) {
log.error('list orgs api error', error);
// NOTE: you can add multiple errors, send all along with a message
setError(form, '', (error as GraphQLError).message);
}
setMessage(form, { type: 'error', message: 'List organizations failed' }, { status: 500 });
return { status: 500, form };
}
const organizations = data?.organizations.map((x) => x.organization);
if (!organizations) error(404, 'organizations not found');
return { organizations, form };
};
*/

export const actions = {
default: async (event) => {
const {
Expand Down
6 changes: 5 additions & 1 deletion apps/console/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ $: if (browser) {
nhost.auth.client.interpreter?.send('SESSION_UPDATE', { data: { session } });
} else {
log.debug('session empty, trigger SIGNOUT');
nhost.auth.client.interpreter?.send('SIGNOUT');
// nhost.auth.client.interpreter?.send('SIGNOUT');
(async () => {
const { error } = await nhost.auth.signOut();
if (error) log.error({ error })
})();
}
}
Expand Down

0 comments on commit adc0c25

Please sign in to comment.