Skip to content

Commit

Permalink
http only cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
arily committed Jul 12, 2023
1 parent d15b8fa commit f5852ca
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 32 deletions.
4 changes: 0 additions & 4 deletions src/middleware/session.global.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { useSession } from '~/store/session'

export default defineNuxtRouteMiddleware(async () => {
const sessionId = useCookie('session')
const session = useSession()
if (!sessionId.value) {
return
}
if (session.loggedIn) {
return
}
Expand Down
4 changes: 0 additions & 4 deletions src/plugins/session.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { useSession } from '~/store/session'

export default defineNuxtPlugin(async () => {
const sessionId = useCookie('session')
const session = useSession()
if (!sessionId.value) {
return
}
await session.retrieve()
})
10 changes: 7 additions & 3 deletions src/server/trpc/middleware/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,15 @@ function createSession(e: H3Event) {
return r
}

const config = {
httpOnly: true,
}

export const sessionProcedure = publicProcedure
.use(async ({ ctx, next }) => {
if (!ctx.session.id) {
const sessionId = await sessionProvider.create(createSession(ctx.h3Event))
setCookie(ctx.h3Event, 'session', sessionId)
setCookie(ctx.h3Event, 'session', sessionId, config)
return await next({
ctx: Object.assign(ctx, {
session: {
Expand All @@ -87,7 +91,7 @@ export const sessionProcedure = publicProcedure
const session = await sessionProvider.get(ctx.session.id)
if (session == null) {
const sessionId = await sessionProvider.create(createSession(ctx.h3Event))
setCookie(ctx.h3Event, 'session', sessionId)
setCookie(ctx.h3Event, 'session', sessionId, config)
return await next({
ctx: Object.assign(ctx, {
session: {
Expand All @@ -105,7 +109,7 @@ export const sessionProcedure = publicProcedure
})
}
if (refreshed !== ctx.session.id) {
setCookie(ctx.h3Event, 'session', refreshed)
setCookie(ctx.h3Event, 'session', refreshed, config)
}

return await next({
Expand Down
20 changes: 4 additions & 16 deletions src/server/trpc/routers/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const router = _router({
}
const newSessionId = await sessionProvider.update(ctx.session.id, { userId: UserProvider.idToString(user.id) })
if (newSessionId && newSessionId !== ctx.session.id) {
setCookie(ctx.h3Event, 'session', newSessionId)
setCookie(ctx.h3Event, 'session', newSessionId, { httpOnly: true })
}
return {
user: mapId(user, UserProvider.idToString),
Expand All @@ -68,21 +68,8 @@ export const router = _router({
}
}),
retrieve: pSession
.input(
z
.object({
sessionId: z.string().optional(),
})
.optional()
)
.query(async ({ ctx, input }) => {
let session
if (input?.sessionId) {
session = await sessionProvider.get(input.sessionId)
}
else {
session = await ctx.session.getBinding()
}
.query(async ({ ctx }) => {
const session = await ctx.session.getBinding()

if (!session) {
throw new TRPCError({
Expand Down Expand Up @@ -110,6 +97,7 @@ export const router = _router({
}
}),
destroy: pSession.mutation(({ ctx }) => {
deleteCookie(ctx.h3Event, 'session')
return sessionProvider.destroy(ctx.session.id)
}),
})
5 changes: 0 additions & 5 deletions src/store/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ export const useSession = defineStore('session', {
async destroy() {
const app$ = useNuxtApp()
app$.$client.session.destroy.mutate()
.then(() => {
this.$reset()
const cookie = useCookie('session')
cookie.value = ''
})
},
async retrieve() {
try {
Expand Down

0 comments on commit f5852ca

Please sign in to comment.