Skip to content

Commit

Permalink
fix: auth attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Maiz27 committed Aug 27, 2023
1 parent 7cc49c7 commit 02f555c
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 115 deletions.
1 change: 0 additions & 1 deletion apps/client/src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ declare global {
type DatabaseUserAttributes = {
email?: string;
github_username?: string;
username?: string;
};
type DatabaseSessionAttributes = Record<string, never>;
}
Expand Down
1 change: 1 addition & 0 deletions apps/client/src/lib/server/lucia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const auth = lucia({
getUserAttributes: (data) => {
return {
github_username: data.github_username,
email: data.email,
};
},
});
Expand Down
18 changes: 9 additions & 9 deletions apps/client/src/routes/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { fail, redirect } from "@sveltejs/kit";
import type { PageServerLoad, Actions } from "./$types";

export const load: PageServerLoad = async ({ locals }) => {
const session = await locals.auth.validate()
const session = await locals.auth.validate();
console.log("session from home", session);
// // const { session, user } = validate()?
// const session = await locals.auth.validate();
Expand All @@ -15,11 +15,11 @@ export const load: PageServerLoad = async ({ locals }) => {
};

export const actions: Actions = {
logout: async ({ locals }) => {
const session = await locals.auth.validate();
if (!session) return fail(401);
await auth.invalidateSession(session.sessionId); // invalidate session
locals.auth.setSession(null); // remove cookie
throw redirect(302, "/login"); // redirect to login page
}
};
logout: async ({ locals }) => {
const session = await locals.auth.validate();
if (!session) return fail(401);
await auth.invalidateSession(session.sessionId); // invalidate session
locals.auth.setSession(null); // remove cookie
throw redirect(302, "/login"); // redirect to login page
},
};
1 change: 0 additions & 1 deletion apps/client/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script>
import { signIn, signOut } from "@auth/sveltekit/client";
import { Card, Button, Tabs } from "svelte-ui";
import { page } from "$app/stores";
import { onMount, onDestroy } from "svelte";
Expand Down
1 change: 0 additions & 1 deletion apps/client/src/routes/Header.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// @ts-nocheck
import { page } from '$app/stores';
import { onMount, onDestroy } from 'svelte';
import { signOut } from '@auth/sveltekit/client';
import 'iconify-icon';
import { Dropdown } from 'svelte-ui';
import { createEventDispatcher } from 'svelte';
Expand Down
17 changes: 12 additions & 5 deletions apps/client/src/routes/[username]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import type { ApolloQueryResult } from "@apollo/client/core";
export const load: PageServerLoad = async (event) => {
const { params, parent } = event;


let githubProfileData = null;
// const currentUser = sessionUser?.username === params?.username;

Expand All @@ -36,7 +35,9 @@ export const load: PageServerLoad = async (event) => {
// sessionUser = data.user[0];
// }

const getUserPinnedItems = (gitHubUsername: string): Promise<ApolloQueryResult<any[]>> => {
const getUserPinnedItems = (
gitHubUsername: string
): Promise<ApolloQueryResult<any[]>> => {
return apolloClient.query({
query: GET_USER_PINNED_ITEMS,
variables: {
Expand All @@ -54,7 +55,9 @@ export const load: PageServerLoad = async (event) => {
});
};

const getRepoSubscriptions = (userId: string): Promise<ApolloQueryResult<any[]>> => {
const getRepoSubscriptions = (
userId: string
): Promise<ApolloQueryResult<any[]>> => {
return apolloClient.query({
query: GET_SUBSCRIBED_REPOS,
variables: {
Expand All @@ -63,7 +66,9 @@ export const load: PageServerLoad = async (event) => {
});
};

const getOwnedRepositories = (gitHubUsername: string): Promise<ApolloQueryResult<any[]>> => {
const getOwnedRepositories = (
gitHubUsername: string
): Promise<ApolloQueryResult<any[]>> => {
return apolloClient.query({
query: GET_USER_REPOS_BY_GITHUB_USERNAME,
variables: {
Expand All @@ -81,7 +86,9 @@ export const load: PageServerLoad = async (event) => {
});
};

const getGitHubAccountInfo = (gitHubLogin: string): Promise<ApolloQueryResult<any[]>> => {
const getGitHubAccountInfo = (
gitHubLogin: string
): Promise<ApolloQueryResult<any[]>> => {
return apolloClient.query({
query: GET_USER_BY_GITHUB_LOGIN,
variables: {
Expand Down
1 change: 0 additions & 1 deletion apps/client/src/routes/authored/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { destructureQueryResults } from "$lib/graphql/helpers";
export const load = async (event) => {
const { parent, data: pageData } = event;


let githubProfileData = null;

const user = await apolloClient.query({
Expand Down
20 changes: 10 additions & 10 deletions apps/client/src/routes/courses/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { fail, redirect } from "@sveltejs/kit";
import type { PageServerLoad, Actions } from "./$types";

export const load: PageServerLoad = async ({ locals }) => {
// console.log("locals from courses:", locals);
const session = await locals.auth.validate()
console.log("courses session", session);
// // const { session, user } = validate()?
// const session = await locals.auth.validate();
// console.log(session);
// // if (!session) throw redirect(302, "/login");
return {
userId: "session.user.userId",
};
// console.log("locals from courses:", locals);
const session = await locals.auth.validate();
console.log("courses session", session);
// // const { session, user } = validate()?
// const session = await locals.auth.validate();
// console.log(session);
// // if (!session) throw redirect(302, "/login");
return {
userId: "session.user.userId",
};
};
3 changes: 2 additions & 1 deletion apps/client/src/routes/login/github/callback/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const GET = async ({ url, cookies, locals }) => {
if (existingUser) return existingUser;
const user = await createUser({
attributes: {
github_username: githubUser?.github_username,
github_username: githubUser?.login,
email: githubUser?.email ?? undefined,
},
});
return user;
Expand Down
4 changes: 1 addition & 3 deletions apps/client/src/routes/messages/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ import { fail, redirect } from "@sveltejs/kit";
import { error } from "@sveltejs/kit";
import type { LayoutServerLoad } from "./$types";

export const load: LayoutServerLoad = async ({ params, parent }) => {

};
export const load: LayoutServerLoad = async ({ params, parent }) => {};
1 change: 0 additions & 1 deletion apps/client/src/routes/messages/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ import type { LayoutServerLoad } from "./$types";

export const load = (async (event) => {
const { parent, fetch, depends, data: pageData } = event;

}) satisfies LayoutServerLoad;
48 changes: 24 additions & 24 deletions apps/client/src/routes/new-user/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,32 @@ import { INSERT_NEW_USER } from "$lib/graphql/mutations/users";
import { mutation } from "svelte-apollo";

const userInfoSchema = z.object({
email: z.string(),
fullName: z.string(),
username: z.string(),
email: z.string(),
fullName: z.string(),
username: z.string(),
});

export const actions: Actions = {
default: async ({ locals, request }) => {
const parsedFormData = parseFormData(await request.formData());
const {
formData: { email, fullName, username },
errors,
} = validateFormData(parsedFormData, userInfoSchema);
const insertNewUser = mutation(INSERT_NEW_USER);
const session = await locals.auth.validate();
default: async ({ locals, request }) => {
const parsedFormData = parseFormData(await request.formData());
const {
formData: { email, fullName, username },
errors,
} = validateFormData(parsedFormData, userInfoSchema);
const insertNewUser = mutation(INSERT_NEW_USER);
const session = await locals.auth.validate();

try {
const result = await insertNewUser({
variables: {
email,
name,
username,
},
});
console.log("Mutation successful!", result);
} catch (error) {
console.log(error);
}
try {
const result = await insertNewUser({
variables: {
email,
name,
username,
},
});
console.log("Mutation successful!", result);
} catch (error) {
console.log(error);
}
}
},
};
1 change: 0 additions & 1 deletion apps/client/src/routes/notifications/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ import type { LayoutServerLoad } from "./$types";

export const load = (async (event) => {
const { parent, fetch, depends, data: pageData } = event;

}) satisfies LayoutServerLoad;
1 change: 0 additions & 1 deletion apps/client/src/routes/repositories/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export const load = async (event) => {
data: pageData,
} = event;


const { data } = await apolloClient.query({
query: GET_USER_BY_EMAIL,
variables: {
Expand Down
Loading

0 comments on commit 02f555c

Please sign in to comment.