-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
315 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { IUserService } from "./../mod/auth/service.ts"; | ||
import { MiddlewareHandler } from "hono"; | ||
import { getCookie } from "hono/cookie"; | ||
import { container, Instances } from "../config/container.ts"; | ||
|
||
const authMiddleware: MiddlewareHandler = async (c, next) => { | ||
const accessToken = getCookie(c, "access_token"); | ||
if (accessToken) { | ||
const srv = container.get<IUserService>(Instances.UserService); | ||
const { data, error } = await srv.getUser(accessToken); | ||
if (data.user) { | ||
c.set("user", { ...data }); | ||
} | ||
if (error) { | ||
return c.json({ error: error.message }, 401); | ||
} | ||
} | ||
await next(); | ||
}; | ||
|
||
export default authMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import type { Context, Env, MiddlewareHandler, TypedResponse } from "hono"; | ||
import type { ZodError, ZodSchema } from "zod"; | ||
|
||
import { ValidationTargets } from "hono"; | ||
import { fromZodError } from "zod-validation-error"; | ||
|
||
import { validator } from "hono/validator"; | ||
import { z } from "zod"; | ||
|
||
export type Hook< | ||
T, | ||
E extends Env, | ||
P extends string, | ||
O = Record<string | number | symbol, never> | ||
> = ( | ||
result: { success: boolean; data: T; error?: ZodError }, | ||
// result: | ||
// | { success: true; data: T } | ||
// | { success: false; error: ZodError; data: T }, | ||
c: Context<E, P> | ||
) => | ||
| Response | ||
| Promise<Response> | ||
| void | ||
| Promise<Response | void | TypedResponse<O>>; | ||
|
||
type HasUndefined<T> = T extends undefined ? true : false; | ||
|
||
export const zValidator = < | ||
T extends ZodSchema, | ||
Target extends keyof ValidationTargets, | ||
E extends Env, | ||
P extends string, | ||
I = z.input<T>, | ||
O = z.output<T>, | ||
V extends { | ||
in: { | ||
[K in Target]: K extends "json" | ||
? I | ||
: { | ||
[x: string]: ValidationTargets[K][string]; | ||
}; | ||
}; | ||
out: { [K in Target]: O }; | ||
} = { | ||
in: { | ||
[K in Target]: K extends "json" | ||
? I | ||
: { | ||
[x: string]: ValidationTargets[K][string]; | ||
}; | ||
}; | ||
out: { [K in Target]: O }; | ||
} | ||
>( | ||
target: Target, | ||
schema: T, | ||
hook?: Hook<z.infer<T>, E, P> | ||
): MiddlewareHandler<E, P, V> => | ||
validator(target, async (v, c) => { | ||
const { success, data, error } = await schema.safeParseAsync(v); | ||
if (hook) { | ||
const hookResult = hook({ success, data, error }, c); | ||
if (hookResult) { | ||
if ( | ||
(hookResult && hookResult instanceof Response) || | ||
hookResult instanceof Promise | ||
) { | ||
return hookResult; | ||
} | ||
if ("response" in hookResult) { | ||
return hookResult["response"]; | ||
} | ||
|
||
if (!success) { | ||
const validationError = fromZodError(error); | ||
|
||
return c.json( | ||
{ | ||
message: validationError.message, | ||
errors: validationError.details, | ||
}, | ||
400 | ||
); | ||
} | ||
} | ||
} | ||
|
||
return data; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { AuthTokenResponsePassword, SupabaseClient, UserResponse } from "@supabase/supabase-js"; | ||
|
||
import { injectable } from "inversify"; | ||
import { supabase } from "../../db/index.ts"; | ||
|
||
export interface IUserRepository { | ||
// auth(): Promise<any>; | ||
signIn(username: string, password: string): Promise<AuthTokenResponsePassword>; | ||
getUser(token: string): Promise<UserResponse>; | ||
} | ||
|
||
@injectable() | ||
export class UserRepository implements IUserRepository { | ||
private _db: SupabaseClient = supabase; | ||
|
||
constructor() {} | ||
|
||
// public async auth() { | ||
// return; | ||
// } | ||
|
||
public async signIn(username: string, password: string) { | ||
return await this._db.auth.signInWithPassword({ | ||
email: username, | ||
password, | ||
}); | ||
} | ||
|
||
public async getUser(token: string) { | ||
return await this._db.auth.getUser(token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { IUserService } from "./service.ts"; | ||
import { Hono } from "hono"; | ||
import { setCookie } from "hono/cookie"; | ||
import { zValidator } from "../../middleware/zodValidator.middleware.ts"; | ||
import { z } from "zod"; | ||
import { container, Instances } from "../../config/container.ts"; | ||
|
||
const authRoutes = new Hono().post( | ||
"/sign-in", | ||
zValidator( | ||
"json", | ||
z.object({ | ||
email: z.string(), | ||
password: z.string(), | ||
}) | ||
), | ||
async (c) => { | ||
const { email, password } = await c.req.valid("json"); | ||
const srv = container.get<IUserService>(Instances.UserService); | ||
const { data, error } = await srv.signIn(email, password); | ||
|
||
if (error) { | ||
return c.json({ error: error.message }, 500); | ||
} | ||
setCookie(c, "access_token", data.session.access_token); | ||
|
||
return c.json(data); | ||
} | ||
); | ||
|
||
export default authRoutes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { AuthTokenResponsePassword, UserResponse } from "@supabase/supabase-js"; | ||
import { IUserRepository } from './repository.ts'; | ||
import { inject, injectable } from "inversify"; | ||
|
||
type User = { | ||
username: string; | ||
password: string; | ||
}; | ||
|
||
export interface IUserService { | ||
signIn(username: string, password: string): Promise<AuthTokenResponsePassword>; | ||
getUser(token: string): Promise<UserResponse>; | ||
} | ||
|
||
@injectable() | ||
export class UserService implements IUserService { | ||
|
||
private _repo: IUserRepository; | ||
|
||
constructor(@inject("UserRepository") repo: IUserRepository) { | ||
this._repo = repo; | ||
} | ||
|
||
public async signIn(username: string, password: string) { | ||
return await this._repo.signIn(username, password); | ||
} | ||
|
||
public async getUser(token: string) { | ||
return await this._repo.getUser(token); | ||
} | ||
|
||
// public async insert(u: User) { | ||
// const { data, error } = await this._db.from("users").insert([u]); | ||
// if (error) { | ||
// console.error(error); | ||
// throw new Error(`Failed to insert user: ${error.message}`); | ||
// } | ||
// return data; | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Hono } from "hono"; | ||
import { container, Instances } from "../../config/container.ts"; | ||
import type { IVocabularyService } from "./service.ts"; | ||
import authMiddleware from "../../middleware/auth.middleware.ts"; | ||
|
||
const vocabularyRoutes = new Hono() | ||
.use("*", authMiddleware) | ||
.post("/create", async (c) => { | ||
const body = await c.req.json<{ word: string }>(); | ||
const srv = container.get<IVocabularyService>(Instances.VocabularyService); | ||
const res = await srv.insert(body.word); | ||
return c.json(res); | ||
}); | ||
|
||
export default vocabularyRoutes; |
Oops, something went wrong.