Skip to content

Commit

Permalink
feat: integrate nuxt-session
Browse files Browse the repository at this point in the history
  • Loading branch information
yuler committed Sep 27, 2023
1 parent c485780 commit 8f71232
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 25 deletions.
27 changes: 17 additions & 10 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
app: {
head: {
link: [{
rel: 'icon',
type: 'image/x-icon',
href: 'logo_64x64.png',
}],
},
},
// Maping `.env` variables
// refs: https://nuxt.com/docs/guide/going-further/runtime-config#environment-variables
runtimeConfig: {
Expand All @@ -10,26 +19,24 @@ export default defineNuxtConfig({
},
modules: [
'@nuxt/ui',
'@vueuse/nuxt',
'@nuxtjs/i18n',
'@vueuse/nuxt',
'@sidebase/nuxt-session',
],
i18n: {
vueI18n: './i18n.config.ts',
// compilation: {
// jit: false,
// },
},
// refs: https://github.com/sidebase/nuxt-session/blob/main/README.md#configuration
session: {
session: {
expiryInSeconds: 60 * 60 * 24 * 7, // 7 days
},
},
build: {
transpile: ['trpc-nuxt'],
},
app: {
head: {
link: [{
rel: 'icon',
type: 'image/x-icon',
href: 'logo_64x64.png',
}],
},
},
devtools: { enabled: true },
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@commitlint/config-conventional": "^17.7.0",
"@nuxt/devtools": "latest",
"@nuxtjs/i18n": "8.0.0-rc.4",
"@sidebase/nuxt-session": "^0.2.7",
"@types/bcryptjs": "^2.4.4",
"husky": "^8.0.3",
"lint-staged": "^14.0.1",
Expand Down
2 changes: 1 addition & 1 deletion pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</script>

<template>
<div class="w-screen h-screen flex flex-col items-center justify-center">
<div class="h-screen flex flex-col items-center justify-center">
<h2>Hi</h2>
<div class="mt-8">
<NuxtLink to="/login">
Expand Down
9 changes: 4 additions & 5 deletions pages/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const schema = z.object({
type Schema = z.output<typeof schema>
const state = ref({
email: undefined,
password: undefined,
email: '[email protected]',
password: 'apibeer',
})
const form = ref<Form<Schema>>()
Expand All @@ -23,8 +23,7 @@ async function onSubmit(event: FormSubmitEvent<Schema>) {
const { email, password } = event.data
submiting.value = true
try {
// TODO: Use `cookie` auth strategy
const _user = await $client.auth.login.mutate({
await $client.auth.login.mutate({
email,
password,
})
Expand Down Expand Up @@ -62,7 +61,7 @@ async function onSubmit(event: FormSubmitEvent<Schema>) {
<UInput v-model="state.email" placeholder="[email protected]" icon="i-heroicons-envelope" />
</UFormGroup>
<UFormGroup label="Password" name="password">
<UInput v-model="state.password" placeholder="123456" icon="i-heroicons-lock-closed" />
<UInput v-model="state.password" placeholder="Your Password" icon="i-heroicons-lock-closed" />
</UFormGroup>
<div class="flex justify-end">
<UButton type="submit" :loading="submiting">
Expand Down
67 changes: 60 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ async function seedUsers() {
// TODO: skip exist
await prisma.user.create({
data: {
username: 'apibeer',
email: '[email protected]',
username: 'apibeer',
password: {
create: { hash: await bcrypt.hash('123456', 10) },
},
Expand Down
3 changes: 3 additions & 0 deletions server/trpc/context.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { User } from '@prisma/client'
import type { Session } from '@sidebase/nuxt-session'
import type { inferAsyncReturnType } from '@trpc/server'
import type { H3Event } from 'h3'

Expand All @@ -14,6 +16,7 @@ export function createContext(event: H3Event) {
*/
return {
prisma: event.context.prisma,
session: event.context.session as Session & { user?: User },
}
}

Expand Down
7 changes: 6 additions & 1 deletion server/trpc/routers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ export const authRouter = router({
password: z.string().min(6),
}),
)
.mutation(async ({ input, ctx }) => {
.mutation(async (event) => {
const { input, ctx } = event
// TODO: implement login
const user = await ctx.prisma.user.findUnique({
where: {
email: input.email,
},
})

// TODO: Wrap trpc error
if (!user)
throw new Error('invalid credentials')

// Storge session
ctx.session.user = user

return user
}),
})

0 comments on commit 8f71232

Please sign in to comment.