Skip to content

Commit

Permalink
feat(nuxt): lucia auth module (#80)
Browse files Browse the repository at this point in the history
* feat(nuxt): lucia auth module

* Fix waitModule return type in module.ts and update definePergelModule in lucia/index.ts

* Add Lucia module to Nuxt config

* Fix lucia package installation

* lucia types

* fix: lint issues

* Update user schema and add auto import for PostgresJS driver

* fix: lint issues

* Refactor database schema and remove unused signup API

* Refactor app.vue, user.ts, csvTest.ts, and middleware.ts

* lint

* Update login page and add error handling for PostgresJS connection

* Update dependencies: lucia, @lucia-auth/adapter-drizzle, and @lucia-auth/adapter-postgresql

* Update Lucia authentication adapters versions

* Add "packages/nuxt/lib" to tsconfig.json exclude list

* Add pergel dependency and create download template

* Remove default branch value in createDownloadTemplate function

* Add support for downloading auth pages template

* Update Pergel configuration and file structure

* Update file paths in eslint.config.js and auth-pages.mjs

* Update file paths in eslint.config.js

* version(nuxt): release 0.5.0-beta.0

* Update package.json and auth-pages.mjs

* version(nuxt): release 0.5.0-beta.1

* Add UI support to rocket project in nuxt.config.ts and update @pergel/nuxt version to 0.5.0-beta.1 in package.json

* Update package versions in examples and playground

* Refactor createDownloadTemplate function

* version(nuxt): release 0.5.0-beta.2

* fix: lint issues

* version(nuxt): release 0.5.0-beta.3

* Fix module validation and update createDownloadTemplate function

* version(nuxt): release 0.5.0-beta.4

* version(nuxt): release 0.5.0-beta.5

* Remove waitModule dependency in SES module

* version(nuxt): release 0.5.0-beta.6

* Update dependencies and versions

* Update Pergel and Lucia modules

* version(nuxt): release 0.5.0-beta.7

* fix: lint issues

* Delete unused files and update dependencies

* Fix UI module dependencies and add consola logger

* version(nuxt): release 0.5.0-beta.8

* fix: lint issues

* version(nuxt): release 0.5.0-beta.9

* version(nuxt): release 0.5.0-beta.10

* Update version and import statement in UI module

* version(nuxt): release 0.5.0-beta.11

* Update dependencies and fix module setup

* version(nuxt): release 0.5.0-beta.12

* Refactor definePergelModule to handle missing dependencies

* version(nuxt): release 0.5.0-beta.13

* Refactor createDownloadTemplate function

* Add DefineDownloadOptions interface to module.ts

* Update package.json and README.yaml, delete auth-pages.mjs, and modify createDownloadTemplate.ts

* version(nuxt): release 0.5.0-beta.14

* version(nuxt): release 0.5.0-beta.15

* Update pergel version to 0.5.0-beta.14
  • Loading branch information
productdevbook authored Jan 1, 2024
1 parent 8ec382d commit 422acc8
Show file tree
Hide file tree
Showing 42 changed files with 1,220 additions and 101 deletions.
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default antfu(
'**/.vscode',
'**/**.yml',
'**/**.md',
'playground/pergel',
'packages/nuxt/playground/pergel',
'packages/nuxt/pergel',
'**/README.yaml',
],
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxt/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ lib
dist
pergel
schema.graphql
!src/**/pergel
!src/**/pergel
!playground/pergel
6 changes: 6 additions & 0 deletions packages/nuxt/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export const external = [
'drizzle-kit',
'postgres',
'@egoist/tailwindcss-icons',
'drizzle',
// #Auth
'oslo',
'lucia',
'@lucia-auth/adapter-drizzle',
'@lucia-auth/adapter-postgresql',
// @ts-expect-error
...Object.keys(pkg.peerDependencies || {}),
]
Expand Down
2 changes: 0 additions & 2 deletions packages/nuxt/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export default defineNuxtConfig({
devtools: true,
pergel: {
pergelDir: 'pergel',
rootDir: 'playground',
debug: true,
projects: {
test: {
Expand All @@ -24,5 +23,4 @@ export default defineNuxtConfig({
},
},
},

})
20 changes: 8 additions & 12 deletions packages/nuxt/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@pergel/nuxt",
"type": "module",
"version": "0.4.4",
"version": "0.5.0-beta.15",
"description": "Nuxt Full Stack modules. Start your next nuxt project fast and strong.",
"author": "Mehmet @productdevbook",
"license": "MIT",
Expand Down Expand Up @@ -37,16 +37,6 @@
"main": "./dist/module.cjs",
"module": "./dist/module.mjs",
"types": "./dist/types.d.ts",
"typesVersions": {
"*": {
"*": [
"./dist/*"
],
"drizzle/*": [
"./lib/drizzle/*"
]
}
},
"files": [
"dist",
"lib"
Expand Down Expand Up @@ -76,14 +66,17 @@
"local-pkg": "^0.5.0",
"minimatch": "^9.0.3",
"pathe": "^1.1.1",
"sirv": "^2.0.4"
"sirv": "^2.0.4",
"typescript": "^5.3.3"
},
"devDependencies": {
"@aws-sdk/client-ses": "^3.484.0",
"@faker-js/faker": "^8.3.1",
"@iconify-json/carbon": "^1.1.27",
"@iconify-json/ph": "^1.1.9",
"@json2csv/node": "^7.0.4",
"@lucia-auth/adapter-drizzle": "1.0.0-beta.4",
"@lucia-auth/adapter-postgresql": "3.0.0-beta.8",
"@nuxt/devtools-ui-kit": "^1.0.6",
"@nuxt/module-builder": "^0.5.5",
"@nuxt/schema": "^3.9.0",
Expand All @@ -95,9 +88,12 @@
"@types/node-cron": "^3.0.11",
"bullmq": "^5.1.0",
"esbuild-plugin-file-path-extensions": "^2.0.0",
"lucia": "3.0.0-beta.13",
"node-cron": "^3.0.3",
"nuxt": "^3.9.0",
"oslo": "^0.26.2",
"p-timeout": "^6.1.2",
"pergel": "workspace:^",
"vitest": "^1.1.1"
},
"publishConfig": {
Expand Down
1 change: 1 addition & 0 deletions packages/nuxt/playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export default defineNuxtConfig({
graphqlYoga: {
mergeSchemas: true,
},
lucia: true,
drizzle: true,
ui: true,
},
Expand Down
8 changes: 6 additions & 2 deletions packages/nuxt/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@
"@aws-sdk/client-ses": "^3.484.0",
"@faker-js/faker": "^8.3.1",
"@json2csv/node": "^7.0.4",
"@lucia-auth/adapter-drizzle": "^1.0.0-beta.4",
"@lucia-auth/adapter-postgresql": "^3.0.0-beta.8",
"@pergel/graphql": "workspace:*",
"@pergel/module-s3": "workspace:*",
"@pergel/module-ui": "workspace:*",
"bullmq": "^5.1.0",
"@pergel/module-ui": "*",
"bullmq": "^4.17.0",
"dotenv": "^16.3.1",
"drizzle-kit": "^0.20.9",
"drizzle-orm": "^0.29.2",
"ioredis": "^5.3.2",
"lucia": "^3.0.0-beta.13",
"node-cron": "^3.0.3",
"oslo": "^0.25.1",
"p-timeout": "^6.1.2",
"postgres": "^3.4.3"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/playground/pages/auth/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const { t } = useI18n()
</template>
<template #content>
<AuthForm
:title="t('auth.signin')"
:title="t('auth.login')"
:description="{
label: t('auth.signup_description'),
to: '/auth/signup',
Expand Down
7 changes: 6 additions & 1 deletion packages/nuxt/playground/pergel/README.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
pergel:
# This file is generated by pergel. Do not edit it manually.
# Version: 0.4.4
# Version: 0.5.0-beta.14
test:
ui:
# If pergel cli is installed, you can run `pergel install` automatically to install
packageJson:
dependencies: "@pergel/module-ui@^0.0.5"
devDependencies: ""
lucia:
# If pergel cli is installed, you can run `pergel install` automatically to install
packageJson:
dependencies: "lucia@^3.0.0-beta.13, oslo@^0.25.1, @lucia-auth/adapter-drizzle@^1.0.0-beta.4, @lucia-auth/adapter-postgresql@^3.0.0-beta.8"
devDependencies: ""
drizzle:
# Script Commands
scripts:
Expand Down
15 changes: 15 additions & 0 deletions packages/nuxt/playground/pergel/templates/auth-pages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "0.1.0",
"templates": {
"pergel-auth": {
"version": "1.0.0",
"branch": "main",
"folder": [
{
"dir": "packages/nuxt/playground/pages/auth",
"output": "pages/auth"
}
]
}
}
}
17 changes: 2 additions & 15 deletions packages/nuxt/playground/pergel/test/drizzle/schema/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,2 @@
// Pergel auto generated please change this file as you wish
import { pgTable, uuid, varchar } from 'drizzle-orm/pg-core'
import { defaultUserSchema } from '@pergel/nuxt/drizzle/schema/pg'
import { sql } from 'drizzle-orm'

export const user = pgTable('user', {
...defaultUserSchema,
})

export const hello = pgTable('hello', {
id: uuid('id').primaryKey().default(sql`uuid_generate_v4()`),
email: varchar('email').unique().notNull(),
password: varchar('password').notNull(),
name: varchar('name').notNull(),
})
export * from './session'
export * from './user'
23 changes: 23 additions & 0 deletions packages/nuxt/playground/pergel/test/drizzle/schema/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { relations } from 'drizzle-orm'
import { pgTable, text, timestamp, uuid } from 'drizzle-orm/pg-core'
import { user } from './user'

export const session = pgTable('session', {
id: text('id').primaryKey(),
expiresAt: timestamp('expiresAt', {
withTimezone: true,
mode: 'date',
}).notNull(),
userId: uuid('userId').notNull()
.references(() => user.id),
})

export const sessionRelation = relations(session, ({ one }) => ({
user: one(user, {
fields: [session.userId],
references: [user.id],
}),
}))

export type Session = (typeof session)['$inferSelect']
export type SessionInsert = (typeof session)['$inferInsert']
31 changes: 31 additions & 0 deletions packages/nuxt/playground/pergel/test/drizzle/schema/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { relations, sql } from 'drizzle-orm'
import { pgEnum, pgTable, text, timestamp, uuid } from 'drizzle-orm/pg-core'
import { session } from './session'

export const roleEnum = pgEnum('role', ['user', 'admin', 'userAdmin'])

export const user = pgTable('user', {
id: uuid('id').primaryKey().default(sql`uuid_generate_v4()`),
name: text('name'),
email: text('email'),
username: text('username').notNull().unique(),
password: text('password').notNull(),
provider: text('provider'),
providerId: text('providerId'),
role: roleEnum('role').notNull().default('user'),
createdAt: timestamp('createdAt', {
withTimezone: true,
mode: 'date',
}).notNull(),
updatedAt: timestamp('updatedAt', {
withTimezone: true,
mode: 'date',
}).notNull(),
})

export const userRelation = relations(user, ({ many }) => ({
sessions: many(session),
}))

export type User = (typeof user)['$inferSelect']
export type UserInsert = (typeof user)['$inferInsert']
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
declare module 'pergel/test/types' {

import type { IncomingMessage, ServerResponse } from 'node:http'
import type { H3Event } from 'h3'
import type { YogaInitialContext } from 'graphql-yoga'
Expand All @@ -8,4 +9,5 @@ declare module 'pergel/test/types' {
req: IncomingMessage
event: H3Event
}

}
11 changes: 11 additions & 0 deletions packages/nuxt/playground/pergel/test/lucia/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { session, user } from 'test/drizzle/schema'

const connect = await pergelTest().drizzle().postgresjs().connect({})

export const auth = pergelTest().lucia().use({
db: connect,
options: {
},
session,
user,
})
19 changes: 19 additions & 0 deletions packages/nuxt/playground/pergel/test/lucia/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Session, User } from 'test/drizzle/schema'
import type { auth } from '#pergel/test/lucia'

declare module 'lucia' {
interface Register {
Lucia: typeof auth
}
interface DatabaseUserAttributes extends Omit<User, 'id'> {}

interface DatabaseSessionAttributes {
}
}

declare module 'h3' {
interface H3EventContext {
user: User | null
session: Session | null
}
}
47 changes: 47 additions & 0 deletions packages/nuxt/playground/server/api/auth/login.post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Argon2id } from 'oslo/password'
import { auth } from '#pergel/test/lucia'

export default eventHandler(async (event) => {
const db = await pergelTest().drizzle().postgresjs().connect({})
const body = await readBody(event)
const username = body.username
if (
typeof username !== 'string'
|| username.length < 3
|| username.length > 31
|| !/^[a-z0-9_-]+$/.test(username)
) {
throw createError({
message: 'Invalid username',
statusCode: 400,
})
}
const password = body.password
if (typeof password !== 'string' || password.length < 6 || password.length > 255) {
throw createError({
message: 'Invalid password',
statusCode: 400,
})
}

const [existingUser] = await db.select()
.from(tablesTest.user).where(eq(tablesTest.user.username, username)).execute()

if (!existingUser) {
throw createError({
message: 'Incorrect username or password',
statusCode: 400,
})
}

const validPassword = await new Argon2id().verify(existingUser.password, password)
if (!validPassword) {
throw createError({
message: 'Incorrect username or password',
statusCode: 400,
})
}

const session = await auth.createSession(existingUser.id, {})
appendHeader(event, 'Set-Cookie', auth.createSessionCookie(session.id).serialize())
})
Loading

0 comments on commit 422acc8

Please sign in to comment.