-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
20b529f
commit 1b34cc9
Showing
17 changed files
with
251 additions
and
262 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
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { RequestEvent } from '@sveltejs/kit' | ||
|
||
export function setSessionTokenCookie( | ||
event: RequestEvent, | ||
token: string, | ||
expiresAt: Date, | ||
): void { | ||
event.cookies.set('session', token, { | ||
httpOnly: true, | ||
sameSite: 'lax', | ||
expires: expiresAt, | ||
path: '/', | ||
}) | ||
} | ||
|
||
export function deleteSessionTokenCookie(event: RequestEvent): void { | ||
event.cookies.set('session', '', { | ||
httpOnly: true, | ||
sameSite: 'lax', | ||
maxAge: 0, | ||
path: '/', | ||
}) | ||
} |
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,10 @@ | ||
import { Google } from 'arctic' | ||
import { GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET } from '$env/static/private' | ||
import { PUBLIC_DOMAIN } from '$env/static/public' | ||
import { dev } from '$app/environment' | ||
|
||
export const google = new Google( | ||
GOOGLE_CLIENT_ID, | ||
GOOGLE_CLIENT_SECRET, | ||
`${dev ? 'http' : 'https'}://${PUBLIC_DOMAIN}/login/google/callback`, | ||
) |
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,88 @@ | ||
import { encodeBase32LowerCaseNoPadding } from '@oslojs/encoding' | ||
import { encodeHexLowerCase } from '@oslojs/encoding' | ||
import { sha256 } from '@oslojs/crypto/sha2' | ||
|
||
import { db } from '$db' | ||
|
||
import { | ||
sessionTable, | ||
userTable, | ||
type SelectUser, | ||
type SelectSession, | ||
} from '$db/schema' | ||
import { eq } from 'drizzle-orm' | ||
|
||
export function generateId(len: number): string { | ||
const bytes = new Uint8Array(len) | ||
crypto.getRandomValues(bytes) | ||
const token = encodeBase32LowerCaseNoPadding(bytes) | ||
return token | ||
} | ||
|
||
export const sessionsC = { | ||
generateSessionToken: function (): string { | ||
const bytes = new Uint8Array(20) | ||
crypto.getRandomValues(bytes) | ||
const token = encodeBase32LowerCaseNoPadding(bytes) | ||
return token | ||
}, | ||
|
||
createSession: async function ( | ||
token: string, | ||
userId: string, | ||
): Promise<SelectSession> { | ||
const sessionId = encodeHexLowerCase( | ||
sha256(new TextEncoder().encode(token)), | ||
) | ||
const session: SelectSession = { | ||
id: sessionId, | ||
userId, | ||
expiresAt: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30), | ||
} | ||
await db.insert(sessionTable).values(session) | ||
return session | ||
}, | ||
|
||
validateSessionToken: async function ( | ||
token: string, | ||
): Promise<SessionValidationResult> { | ||
const sessionId = encodeHexLowerCase( | ||
sha256(new TextEncoder().encode(token)), | ||
) | ||
const result = await db | ||
.select({ user: userTable, session: sessionTable }) | ||
.from(sessionTable) | ||
.innerJoin(userTable, eq(sessionTable.userId, userTable.id)) | ||
.where(eq(sessionTable.id, sessionId)) | ||
if (result.length < 1) { | ||
return { session: null, user: null } | ||
} | ||
const { user, session } = result[0] | ||
if (Date.now() >= session.expiresAt.getTime()) { | ||
await db.delete(sessionTable).where(eq(sessionTable.id, session.id)) | ||
return { session: null, user: null } | ||
} | ||
if (Date.now() >= session.expiresAt.getTime() - 1000 * 60 * 60 * 24 * 15) { | ||
session.expiresAt = new Date(Date.now() + 1000 * 60 * 60 * 24 * 30) | ||
await db | ||
.update(sessionTable) | ||
.set({ | ||
expiresAt: session.expiresAt, | ||
}) | ||
.where(eq(sessionTable.id, session.id)) | ||
} | ||
return { session, user } | ||
}, | ||
|
||
invalidateSession: async function (sessionId: string): Promise<void> { | ||
await db.delete(sessionTable).where(eq(sessionTable.id, sessionId)) | ||
}, | ||
|
||
invalidateUserSessions: async function (userId: string): Promise<void> { | ||
await db.delete(sessionTable).where(eq(sessionTable.userId, userId)) | ||
}, | ||
} | ||
|
||
export type SessionValidationResult = | ||
| { session: SelectSession; user: SelectUser } | ||
| { session: null; user: null } |
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,84 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { | ||
sqliteTable, | ||
text, | ||
integer, | ||
// customType, | ||
} from 'drizzle-orm/sqlite-core' | ||
import { sql, relations } from 'drizzle-orm' | ||
|
||
import { userTable, productItemTable } from '$db/schema' | ||
import { createInsertSchema } from 'drizzle-zod' | ||
|
||
export const addressTable = sqliteTable('address', { | ||
id: integer('id').notNull().primaryKey({ autoIncrement: true }), | ||
created_at: integer('created_at', { mode: 'timestamp' }).default( | ||
sql`(CURRENT_TIMESTAMP)`, | ||
), | ||
|
||
user_id: text('user_id') | ||
.notNull() | ||
.references(() => userTable.id), | ||
is_default: integer('is_default', { mode: 'boolean' }).default(false), | ||
cep: text('cep').notNull(), | ||
street: text('street').notNull(), | ||
number: text('number').notNull(), | ||
complement: text('complement').notNull(), | ||
neighborhood: text('neighborhood').notNull(), | ||
city: text('city').notNull(), | ||
state: text('state').notNull(), | ||
country: text('country').notNull(), | ||
}) | ||
|
||
export const insertAddressSchema = createInsertSchema(addressTable) | ||
export type SelectAddress = typeof addressTable.$inferSelect | ||
export type InsertAddress = typeof addressTable.$inferInsert | ||
|
||
export const customerOrderTable = sqliteTable('customer_order', { | ||
id: integer('id').notNull().primaryKey({ autoIncrement: true }), | ||
// .$defaultFn(() => generateId(15)), | ||
created_at: integer('created_at', { mode: 'timestamp' }).default( | ||
sql`(CURRENT_TIMESTAMP)`, | ||
), | ||
|
||
user_id: text('customer_id') | ||
.notNull() | ||
.references(() => userTable.id), | ||
address_id: integer('address_id').references(() => addressTable.id), | ||
payment_method: text('payment_method').notNull(), | ||
total: integer('total').notNull(), | ||
observation: text('observation'), | ||
status: text('status', { | ||
enum: [ | ||
'PENDING', | ||
'CONFIRMED', | ||
'PREPARING', | ||
'ON THE WAY', | ||
'DELIVERED', | ||
'CANCELED', | ||
], | ||
}).notNull(), | ||
}) | ||
|
||
export type SelectCustomerOrder = typeof customerOrderTable.$inferSelect | ||
export type InsertCustomerOrder = typeof customerOrderTable.$inferInsert | ||
|
||
export const orderItemTable = sqliteTable('order_item', { | ||
id: integer('id').notNull().primaryKey({ autoIncrement: true }), | ||
created_at: integer('created_at', { mode: 'timestamp' }).default( | ||
sql`(CURRENT_TIMESTAMP)`, | ||
), | ||
|
||
order_id: integer('order_id') | ||
.notNull() | ||
.references(() => customerOrderTable.id), | ||
product_id: integer('product_id') | ||
.notNull() | ||
.references(() => productItemTable.id), | ||
observation: text('observation'), | ||
quantity: integer('quantity').notNull(), | ||
price: integer('price').notNull(), | ||
}) | ||
|
||
export type SelectOrderItem = typeof orderItemTable.$inferSelect | ||
export type InsertOrderItem = typeof orderItemTable.$inferInsert |
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
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
Oops, something went wrong.