Backend added user authentication (without session yet)

pull/3/head
TZGyn 2 years ago
parent c1bb67e55e
commit c8f466fe2a
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

Binary file not shown.

@ -0,0 +1,53 @@
import { nanoid } from 'nanoid'
import { db } from './database'
export const signup = async (
email: string,
username: string,
password: string,
password_confirm: string
) => {
if (password !== password_confirm) {
return { error: 'password is not the same' }
}
if (password.length < 8) {
return { error: 'password should be at least length 8' }
}
try {
await db
.insertInto('user')
.values({
uuid: nanoid(16),
email,
username,
password: await Bun.password.hash(password),
})
.execute()
return { error: undefined }
} catch (error) {
console.log(error)
return { error: 'error' }
}
}
export const login = async (email: string, password: string) => {
const userArray = await db
.selectFrom('user')
.selectAll()
.where('user.email', '=', email)
.execute()
if (userArray.length < 1) {
return { error: 'Invalid User' }
}
const user = userArray[0]
if (await Bun.password.verify(password, user.password)) {
return { user }
} else {
return { error: 'Incorrect Credentials' }
}
}

@ -1,9 +1,10 @@
import { Elysia } from 'elysia'
import { Elysia, t } from 'elysia'
import { nanoid } from 'nanoid'
import { db } from './database'
import { createLinkSchema } from './zodSchema'
import { cors } from '@elysiajs/cors'
import { jsonArrayFrom } from 'kysely/helpers/postgres'
import { login, signup } from './auth'
const fallback_url = Bun.env.FALLBACK_URL ?? 'https://shortener.tzgyn.com'
@ -89,42 +90,98 @@ app.get(
)
app.get('/link/:shortenerCode', async ({ params: { shortenerCode } }) => {
const shorteners = await db
.selectFrom('shortener')
.select((shortener) => [
'id',
'code',
'link',
'created_at',
jsonArrayFrom(
shortener
.selectFrom('visitor')
.select([
'visitor.created_at as visited_at',
'visitor.country_code',
])
.whereRef('visitor.shortener_id', '=', 'shortener.id')
).as('visitors'),
])
.where('code', '=', shortenerCode)
.execute()
try {
const shorteners = await db
.selectFrom('shortener')
.select((shortener) => [
'id',
'code',
'link',
'created_at',
jsonArrayFrom(
shortener
.selectFrom('visitor')
.select([
'visitor.created_at as visited_at',
'visitor.country_code',
])
.whereRef('visitor.shortener_id', '=', 'shortener.id')
).as('visitors'),
])
.where('code', '=', shortenerCode)
.execute()
const visitors = await db
.selectFrom('visitor')
.select(({ fn }) => [
'visitor.country_code',
'visitor.country',
fn.count<number>('visitor.id').as('visitor_count'),
])
.where('visitor.shortener_id', '=', shorteners[0].id)
.groupBy(['visitor.country_code', 'visitor.country'])
.execute()
return { shorteners, visitors }
} catch {
return { error: true }
}
})
const visitors = await db
.selectFrom('visitor')
.select(({ fn }) => [
'visitor.country_code',
'visitor.country',
fn.count<number>('visitor.id').as('visitor_count'),
])
.where('visitor.shortener_id', '=', shorteners[0].id)
.groupBy(['visitor.country_code', 'visitor.country'])
.execute()
app.post(
'/signup',
async ({ body, set }) => {
const { email, username, password, password_confirm } = body
const { error } = await signup(
email,
username,
password,
password_confirm
)
if (error) {
set.status = 400
return { error }
}
return { shorteners, visitors }
})
return { message: 'User Successfully Created' }
},
{
body: t.Object({
username: t.String(),
email: t.String(),
password: t.String(),
password_confirm: t.String(),
}),
}
)
app.post(
'/login',
async ({ body, set }) => {
const { email, password } = body
const { user, error } = await login(email, password)
if (error) {
set.status = 400
return { error }
} else {
return user
}
},
{
body: t.Object({
email: t.String(),
password: t.String(),
}),
}
)
app.listen(3000)
console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
)
export type App = typeof app

@ -6,9 +6,12 @@ import {
Updateable,
} from 'kysely'
export type Timestamp = ColumnType<Date, Date | string, Date | string>
export interface Database {
shortener: ShortenerTable
visitor: VisitorTable
user: UserTable
}
export interface ShortenerTable {
@ -32,3 +35,16 @@ export interface VisitorTable {
export type Visitor = Selectable<VisitorTable>
export type NewVisitor = Insertable<VisitorTable>
export interface UserTable {
created_at: Generated<Timestamp>
email: string
id: Generated<number>
password: string
username: string
uuid: string
}
export type User = Selectable<UserTable>
export type NewUser = Insertable<UserTable>
export type UserUpdate = Updateable<UserTable>

Loading…
Cancel
Save