v1.0
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import * as cookie from "cookie";
|
||||
import { z } from "zod";
|
||||
import { Session } from "@contracts/constants";
|
||||
import { getSessionCookieOptions } from "./lib/cookies";
|
||||
import { createRouter, authedQuery, publicQuery } from "./middleware";
|
||||
import { loginWithCredentials, setSessionCookie } from "./auth";
|
||||
|
||||
export const authRouter = createRouter({
|
||||
me: authedQuery.query((opts) => opts.ctx.user),
|
||||
login: publicQuery
|
||||
.input(
|
||||
z.object({
|
||||
identifier: z.string().min(1),
|
||||
password: z.string().min(1),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const { user, token } = await loginWithCredentials(
|
||||
input.identifier,
|
||||
input.password,
|
||||
);
|
||||
|
||||
setSessionCookie(ctx.resHeaders, token);
|
||||
return { user };
|
||||
}),
|
||||
logout: authedQuery.mutation(async ({ ctx }) => {
|
||||
const opts = getSessionCookieOptions(ctx.req.headers);
|
||||
ctx.resHeaders.append(
|
||||
"set-cookie",
|
||||
cookie.serialize(Session.cookieName, "", {
|
||||
httpOnly: opts.httpOnly,
|
||||
path: opts.path,
|
||||
sameSite: opts.sameSite?.toLowerCase() as "lax" | "none",
|
||||
secure: opts.secure,
|
||||
maxAge: 0,
|
||||
}),
|
||||
);
|
||||
return { success: true };
|
||||
}),
|
||||
});
|
||||
Reference in New Issue
Block a user