π The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.
Vue dockergolangkanbanknowledge-baseknowledge-managementmarkdownnote-takingnotesnotes-appnotionnuxtnuxtjsoffline-firstoidcpwaselfhostedssovuevuejswiki
Add account creation
Smaug6739b4d10de2d78a
diff --git a/backend/classes/UsersManager.ts b/backend/classes/UsersManager.tsindex 557980ef..1c2d492c 100644--- a/backend/classes/UsersManager.ts+++ b/backend/classes/UsersManager.ts@@ -8,8 +8,8 @@ export class UsersManager extends Base {}public validator = new Validator({- id: { minLength: 1, maxLength: 50, type: 'string', error: 'user id invalid' },- username: { minLength: 1, maxLength: 25, type: 'string', error: 'user username invalid' },+ id: { maxLength: 50, type: 'string', error: 'user id invalid' },+ username: { minLength: 5, maxLength: 25, type: 'string', error: 'user username invalid' },firstname: { maxLength: 25, type: 'string', error: 'user firstname invalid', optional: true },lastname: { maxLength: 25, type: 'string', error: 'user lastname invalid', optional: true },avatar: { maxLength: 75, type: 'string', error: 'user avatar invalid', optional: true },@@ -28,6 +28,14 @@ export class UsersManager extends Base {});});}+ public exists(username: string): Promise<boolean> {+ return new Promise((resolve, reject) => {+ this.app.db.query<UserDB[]>('SELECT id FROM users WHERE username = ? LIMIT 1', [username], async (err, results) => {+ if (err) return reject('Internal database error.');+ resolve(!!results[0]);+ });+ });+ }public getPublic(id: string): Promise<UserDB> {return new Promise((resolve, reject) => {this.app.db.query<UserDB[]>(@@ -50,6 +58,30 @@ export class UsersManager extends Base {});});}+ public post(data: User) {+ return new Promise((resolve, reject) => {+ const error = this.validator.validate(data);+ if (error) return reject(error);+ this.app.db.query<UserDB[]>(+ 'INSERT INTO users (id, username, firstname, lastname, avatar, email, password, created_timestamp, updated_timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',+ [+ data.id,+ data.username,+ data.firstname,+ data.lastname,+ data.avatar,+ data.email,+ data.password,+ data.created_timestamp,+ data.updated_timestamp,+ ],+ err => {+ if (err) return reject('Internal database error.');+ resolve(data);+ },+ );+ });+ }public put(id: string, data: Omit<User, 'id' | 'password' | 'created_timestamp'>) {return new Promise((resolve, reject) => {const error = this.validator.validate(data);diff --git a/backend/controllers/users.controller.ts b/backend/controllers/users.controller.tsindex 6372812f..2663231b 100644--- a/backend/controllers/users.controller.ts+++ b/backend/controllers/users.controller.ts@@ -19,6 +19,32 @@ export default class Authentification extends BaseController<UsersManager> {res.status(500).json(this.utils.error(e));}}+ public async post(req: Request, res: Response) {+ try {+ const passwordStr = req.body.password as string;+ if (!passwordStr) return res.status(400).json(this.utils.error('Password must be provided.'));+ const hashed = await hash(passwordStr, 10);+ const user = await this.manager.exists(req.body.username || '');+ if (user) return res.status(400).json(this.utils.error('Username already taken.'));+ const data = {+ id: this.app.snowflake.generate().toString(),+ username: req.body.username as string,+ firstname: req.body.firstname as string,+ lastname: req.body.lastname as string,+ avatar: req.body.avatar as string,+ email: req.body.email as string,+ password: hashed,+ created_timestamp: Date.now().toString(),+ updated_timestamp: Date.now().toString(),+ };+ const error = this.manager.validator.validate(data);+ if (error) return res.status(400).json(this.utils.error(error));+ const r = await this.manager.post(data);+ return res.status(201).json(this.utils.success(r));+ } catch (e) {+ return res.status(500).json(this.utils.error(e));+ }+ }public async update(req: Request, res: Response) {const user_db = await this.manager.getById(req.params.id!);if (!user_db) return res.status(404).json(this.utils.error('User not found.'));diff --git a/backend/routes/v1/users.ts b/backend/routes/v1/users.tsindex 9a2deaa0..1bb2e7e0 100644--- a/backend/routes/v1/users.ts+++ b/backend/routes/v1/users.ts@@ -12,6 +12,7 @@ export default (client: App): Iroute => {router() {UsersRouter.get('/@me', authMid, (req, res) => controller.getMe(req, res));UsersRouter.patch('/:id/password', authMid, (req, res) => controller.updatePassword(req, res));+ UsersRouter.post('/', (req, res) => controller.post(req, res));UsersRouter.patch('/:id', authMid, (req, res) => controller.update(req, res));//UsersRouter.get('/:id', authMid, (req, res) => controller.getDocument(req, res));diff --git a/frontend/pages/index.vue b/frontend/pages/index.vueindex a66a9e7b..b722918a 100644--- a/frontend/pages/index.vue+++ b/frontend/pages/index.vue@@ -4,7 +4,7 @@<div class="container"></div><div class="actions"><NuxtLink to="/login" class="login">Login</NuxtLink>- <NuxtLink to="/" class="sign-up">Sign up</NuxtLink>+ <NuxtLink to="/signup" class="sign-up">Sign up</NuxtLink></div></nav><main>diff --git a/frontend/pages/signup.vue b/frontend/pages/signup.vuenew file mode 100644index 00000000..7b15e0cd--- /dev/null+++ b/frontend/pages/signup.vue@@ -0,0 +1,192 @@+<template>+ <div class="container">+ <form @submit.prevent="register" class="body">+ <p class="login-link">Already have an account? <a href="/login">Log in</a></p>+ <div class="form-group">+ <label for="username">Username</label>+ <input type="text" id="username" v-model="username" :class="{ 'is-invalid': errors.username }" />+ <p v-if="errors.username" class="invalid-feedback">{{ errors.username }}</p>+ </div>+ <div class="form-group">+ <label for="email">Email</label>+ <input type="email" id="email" v-model="email" :class="{ 'is-invalid': errors.email }" />+ <p v-if="errors.email" class="invalid-feedback">{{ errors.email }}</p>+ </div>+ <div class="form-group">+ <label for="password">Password</label>+ <div class="password-input">+ <input :type="showPassword ? 'text' : 'password'" id="password" v-model="password" :class="{ 'is-invalid': errors.password }" />+ <button type="button" class="password-toggle" @click="showPassword = !showPassword">{{ showPassword ? 'Hide' : 'Show' }}</button>+ </div>+ <p v-if="errors.password" class="invalid-feedback">{{ errors.password }}</p>+ </div>+ <div class="form-group">+ <label for="confirmPassword">Confirm Password</label>+ <div class="password-input">+ <input :type="showConfirmPassword ? 'text' : 'password'" id="confirmPassword" v-model="confirmPassword" :class="{ 'is-invalid': errors.confirmPassword }" />+ <button type="button" class="password-toggle" @click="showConfirmPassword = !showConfirmPassword">{{ showConfirmPassword ? 'Hide' : 'Show' }}</button>+ </div>+ <p v-if="errors.confirmPassword" class="invalid-feedback">{{ errors.confirmPassword }}</p>+ </div>+ <button type="submit" class="btn">Sign Up</button>+ <p v-if="errors.general" class="invalid-feedback">{{ errors.general }}</p>+ </form>+ </div>+</template>++<script setup lang="ts">+const router = useRouter();+const username = ref('');+const email = ref('');+const password = ref('');+const confirmPassword = ref('');+const showPassword = ref(false);+const showConfirmPassword = ref(false);+const errors = ref({+ username: '',+ email: '',+ password: '',+ confirmPassword: '',+ general: '',+});+const userStore = useUserStore();++function register() {+ let valid = true;++ if (!username.value) {+ errors.value.username = 'Username is required';+ valid = false;+ } else {+ errors.value.username = '';+ }++ if (!email.value) {+ errors.value.email = 'Email is required';+ valid = false;+ } else {+ errors.value.email = '';+ }++ if (!password.value) {+ errors.value.password = 'Password is required';+ valid = false;+ } else {+ errors.value.password = '';+ }++ if (!confirmPassword.value) {+ errors.value.confirmPassword = 'Password confirmation is required';+ valid = false;+ } else if (password.value !== confirmPassword.value) {+ errors.value.confirmPassword = 'Passwords do not match';+ valid = false;+ } else {+ errors.value.confirmPassword = '';+ }++ if (valid) {+ createAccount(username.value, email.value, password.value);+ }+}++async function createAccount(username: string, email: string, password: string) {+ userStore+ .register({ username, email, password })+ .then(() => {+ router.push('/login');+ })+ .catch(error => {+ console.log('---------------', error);++ errors.value.general = error;+ });+}+</script>++<style scoped lang="scss">+.container {+ display: flex;+ flex-direction: column;+ align-items: center;+ height: 100%;+ justify-content: center;+}++.body {+ display: flex;+ flex-direction: column;+ align-items: center;+ width: 100%;+ max-width: 620px;+ padding: 3rem;+ background-color: var(--bg-contrast);+ border-radius: 15px;+}++.form-group {+ display: flex;+ flex-direction: column;+ width: 100%;+ margin-bottom: 1rem;+}++.login-link {+ font-size: 0.8rem;+ margin-top: -0.5rem;+ margin-bottom: 1rem;+ text-align: right;+ text-decoration: none;++ &:hover {+ text-decoration: underline;+ }+}++.password-input {+ position: relative;+}++.password-toggle {+ position: absolute;+ top: 50%;+ right: 0;+ transform: translateY(-50%);+ font-size: 0.8rem;+ padding: 0.2rem;+ background-color: transparent;+ border: none;+ cursor: pointer;+ outline: none;++ &:hover {+ text-decoration: underline;+ }+}++.btn {+ font-size: 1.2rem;+ border-radius: 50px;+ width: 100%;+ background-color: $primary-300;++ &:hover {+ background: $primary-500;+ transform: none;+ }+}++input {+ background: var(--bg-contrast-2);+ outline: none;+}++.is-invalid {+ border-color: $red !important;+}++.invalid-feedback {+ font-size: 0.8rem;+ color: $red;+}+</style>diff --git a/frontend/stores/db_strustures.d.ts b/frontend/stores/db_strustures.d.tsindex f9d83179..c5620a86 100644--- a/frontend/stores/db_strustures.d.ts+++ b/frontend/stores/db_strustures.d.ts@@ -33,6 +33,7 @@ export interface User {firstname?: string;lastname?: string;avatar?: string;+ password?: string;email: string;created_timestamp: string;}diff --git a/frontend/stores/user.store.ts b/frontend/stores/user.store.tsindex df1b51fa..23eca9a8 100644--- a/frontend/stores/user.store.ts+++ b/frontend/stores/user.store.ts@@ -22,6 +22,12 @@ export const useUserStore = defineStore('user', {logout() {if (import.meta.client) localStorage.removeItem('isLoggedIn');},+ register(user: Omit<User, 'id' | 'created_timestamp'>) {+ return new Promise(async (resolve, reject) => {+ const request = await makeRequest('users', 'POST', user);+ request.status === 'success' ? resolve(true) : reject(request.message);+ });+ },fetch() {return new Promise(async (resolve, reject) => {const responce = await makeRequest<{ user: User; last_connection: ConnectionLog }>(`users/@me`, 'GET', {});