Skip to content

Commit

Permalink
feat: add email to users
Browse files Browse the repository at this point in the history
  • Loading branch information
EnzoDOROSARIO committed Dec 14, 2024
1 parent b18bd1a commit b1bfded
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions migrations/006.do.user_email.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE users ADD COLUMN email VARCHAR(255) NOT NULL;
1 change: 1 addition & 0 deletions migrations/006.undo.user_email.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE users DROP COLUMN email;
16 changes: 10 additions & 6 deletions scripts/seed-database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,29 @@ async function truncateTables (connection: Connection) {
}

async function seedUsers (connection: Connection) {
const usernames = ['basic', 'moderator', 'admin']
const users = [
{ username: 'basic', email: '[email protected]' },
{ username: 'moderator', email: '[email protected]' },
{ username: 'admin', email: '[email protected]' }
]
const hash = await scryptHash('password123$')

// The goal here is to create a role hierarchy
// E.g. an admin should have all the roles
const rolesAccumulator: number[] = []

for (const username of usernames) {
for (const user of users) {
const [userResult] = await connection.execute(`
INSERT INTO users (username, password)
VALUES (?, ?)
`, [username, hash])
INSERT INTO users (username, email, password)
VALUES (?, ?, ?)
`, [user.username, user.email, hash])

const userId = (userResult as { insertId: number }).insertId

const [roleResult] = await connection.execute(`
INSERT INTO roles (name)
VALUES (?)
`, [username])
`, [user.username])

const newRoleId = (roleResult as { insertId: number }).insertId

Expand Down
10 changes: 7 additions & 3 deletions src/routes/api/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import {
FastifyPluginAsyncTypebox,
Type
} from '@fastify/type-provider-typebox'
import { CredentialsSchema, Credentials } from '../../../schemas/auth.js'
import { Credentials } from '../../../schemas/auth.js'

const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
fastify.post(
'/login',
{
schema: {
body: CredentialsSchema,
body: Type.Object({
username: Type.String(),
password: Type.String()
}),
response: {
200: Type.Object({
success: Type.Boolean(),
Expand All @@ -27,7 +30,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {

return fastify.knex.transaction(async (trx) => {
const user = await trx<Credentials>('users')
.select('username', 'password')
.select('username', 'email', 'password')
.where({ username })
.first()

Expand All @@ -45,6 +48,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {

request.session.user = {
username,
email: user.email,
roles: roles.map((role) => role.name)
}

Expand Down
1 change: 1 addition & 0 deletions src/schemas/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Static, Type } from '@sinclair/typebox'

export const CredentialsSchema = Type.Object({
username: Type.String(),
email: Type.String({ format: 'email' }),
password: Type.String()
})

Expand Down
1 change: 1 addition & 0 deletions test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ async function login (this: FastifyInstance, username: string) {
url: '/api/auth/login',
payload: {
username,
email: '[email protected]',
password: 'password123$'
}
})
Expand Down
4 changes: 3 additions & 1 deletion test/routes/api/tasks/tasks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import os from 'os'

async function createUser (
app: FastifyInstance,
userData: Partial<{ username: string; password: string }>
userData: Partial<{ username: string; email: string; password: string }>
) {
const [id] = await app.knex('users').insert(userData)
return id
Expand Down Expand Up @@ -52,10 +52,12 @@ describe('Tasks api (logged user only)', () => {

userId1 = await createUser(app, {
username: 'user1',
email: '[email protected]',
password: 'password1'
})
userId2 = await createUser(app, {
username: 'user2',
email: '[email protected]',
password: 'password2'
})

Expand Down

0 comments on commit b1bfded

Please sign in to comment.