Skip to content

Commit

Permalink
add firebase admin.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bui Thanh Luan committed Oct 10, 2022
1 parent 5fb3589 commit b248f57
Show file tree
Hide file tree
Showing 12 changed files with 839 additions and 55 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ node_modules
!.env.example
.vercel
.output

# key
firebaseKey.json
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"endOfLine": "auto",
"htmlWhitespaceSensitivity": "ignore",
"arrowParens": "always",
"bracketSpacing": true
"bracketSpacing": true,
"tabWidth": 2
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"daisyui": "^2.31.0",
"dayjs": "^1.11.5",
"firebase": "^9.11.0",
"firebase-admin": "^11.1.0",
"typesafe-i18n": "^5.14.0",
"xlsx": "^0.18.5"
}
Expand Down
2 changes: 1 addition & 1 deletion src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
%sveltekit.head%
</head>
<body>
<body data-sveltekit-prefetch>
<div>%sveltekit.body%</div>
</body>
</html>
6 changes: 6 additions & 0 deletions src/lib/firebase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { initializeApp, credential } from 'firebase-admin'
import { PATH_TO_KEY } from '$env/static/private'

export const app = initializeApp({
credential: credential.cert(PATH_TO_KEY),
})
5 changes: 2 additions & 3 deletions src/routes/[lang]/explore/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import type { PageServerLoad } from './$types'
import { getTopTags } from '$lib/db/tag'

export const load: PageServerLoad = async () => {
const tags = await getTopTags()
return {
tags,
tags: getTopTags(),
}
}
}
1 change: 1 addition & 0 deletions src/routes/[lang]/login/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Login</h1>
35 changes: 31 additions & 4 deletions src/routes/[lang]/profile/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
import type { PageServerLoad } from './$types';
import { redirect } from '@sveltejs/kit'
import type { PageServerLoad } from './$types'
import { getAuth } from 'firebase-admin/auth'

export const load: PageServerLoad = async () => {
return {};
};
import { app } from '$lib/firebase'
import { prisma } from '$lib/prisma'

export const load: PageServerLoad = async ({ cookies, locals: { locale } }) => {
try {
const token = cookies.get('token')
if (!token) {
throw redirect(307, `/${locale}/login`)
}
const { uid } = await getAuth(app).verifyIdToken(
'eyJhbGciOiJSUzI1NiIsImtpZCI6Ijk5NjJmMDRmZWVkOTU0NWNlMjEzNGFiNTRjZWVmNTgxYWYyNGJhZmYiLCJ0eXAiOiJKV1QifQ.eyJuYW1lIjoiTHXDom4gQsO5aSIsInBpY3R1cmUiOiJodHRwczovL2xoMy5nb29nbGV1c2VyY29udGVudC5jb20vYS9BSXRidm1tWnZ5X0lRX2N6aFdfUmFtSVlXdEc2aDV2bjI0VVg0eFlOME5xXz1zOTYtYyIsImlzcyI6Imh0dHBzOi8vc2VjdXJldG9rZW4uZ29vZ2xlLmNvbS9iYXlxdWFtb24iLCJhdWQiOiJiYXlxdWFtb24iLCJhdXRoX3RpbWUiOjE2NjUzOTYyODEsInVzZXJfaWQiOiJqckdlQzVLUGgzUkVyTUVhZU5NU1hZa0hMSUEyIiwic3ViIjoianJHZUM1S1BoM1JFck1FYWVOTVNYWWtITElBMiIsImlhdCI6MTY2NTM5NjI4MSwiZXhwIjoxNjY1Mzk5ODgxLCJlbWFpbCI6Imx1YW4udnkueWJAZ21haWwuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImZpcmViYXNlIjp7ImlkZW50aXRpZXMiOnsiZ29vZ2xlLmNvbSI6WyIxMDI4NzI3MDUwODE1Mjg5NjM5MDIiXSwiZW1haWwiOlsibHVhbi52eS55YkBnbWFpbC5jb20iXX0sInNpZ25faW5fcHJvdmlkZXIiOiJnb29nbGUuY29tIn19.DOi7Usu0Gg4V8765U6xf-gEFnSeplf05IUdaeQV0LiH2TPYvaVgghVZFHT55iPn86dDBgmx8Raal5iC_7t_8QxrHZfGH0Noe7YuHyXivItCvC2FphfaVBQ4lM0-SE5axZgqltckj24qLIMbQ6jJs9HkwJLMzR2epS0UOpoCMtBfFe8SdAH32L8OuXrBrGTg8wVxCkRP7-_Dc5KxhBCkiKF9K46xyxWcWhhphR9btHvZyKEwMM4sAJzdFuqHdEX1Xgl63aO9ol541EGG-mlpt4XGk8ASPHYHlzzhowIHK_LDl5eWTua_plv9Ra30BZmdx6K7zs7se7p6Wqxiio_ajZQ',
true,
)
console.log(uid)

return {
user: prisma.user.findFirst({
where: {
uid: '12345',
},
}),
}
} catch (e) {
console.log(e)

throw redirect(307, `/${locale}/login`)
}
}
9 changes: 5 additions & 4 deletions src/routes/[lang]/profile/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script lang="ts">
import type { PageData } from './$types';
export let data: PageData;
import type { PageData } from './$types'
export let data: PageData
</script>

<h1>Profile</h1>
<h1>Profile</h1>
{JSON.stringify(data, null, 2)}
8 changes: 4 additions & 4 deletions src/routes/[lang]/profile/+page.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PageLoad } from './$types';
import type { PageLoad } from './$types'

export const load: PageLoad = async () => {
return {};
};
export const load: PageLoad = async ({ data }) => {
return data
}
17 changes: 11 additions & 6 deletions src/stores/auth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { readable } from 'svelte/store';
import { getAuth, type User } from 'firebase/auth';
import { readable } from 'svelte/store'
import { getAuth, type User } from 'firebase/auth'
import { browser } from '$app/environment'

export const user = readable<User | null>(null, (set) => {
getAuth().onAuthStateChanged((user) => {
set(user);
});
});
getAuth().onAuthStateChanged(async (user) => {
const token = await user?.getIdToken()
if (browser) {
document.cookie = `token=${token ?? ''}; path=/`
}
set(user)
})
})
Loading

0 comments on commit b248f57

Please sign in to comment.