From 997e85c038ea3d0dde74201f9284926a5c11e85a Mon Sep 17 00:00:00 2001 From: Jan Wolfram Date: Tue, 13 Sep 2022 09:11:16 +0200 Subject: [PATCH 1/7] feat: register method --- lib/api/index.js | 14 ++++++++++++++ lib/middleware.js | 2 ++ lib/module.js | 2 ++ lib/plugin.js | 25 +++++++++++++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/lib/api/index.js b/lib/api/index.js index c587a2b..934373e 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -40,6 +40,7 @@ class Api { baseUrl = null, accessTokenEndpoint = null, refreshTokenEndpoint = null, + registerEndpoint = null, mercureUrl = null, usersEntity = null, accessTokenUserIdKey = null, @@ -52,6 +53,7 @@ class Api { this.jwt = jwt; this.baseUrl = baseUrl; this.accessTokenEndpoint = accessTokenEndpoint; + this.registerEndpoint = registerEndpoint; this.refreshTokenEndpoint = refreshTokenEndpoint; this.mercureUrl = mercureUrl; this.eventSource = null; @@ -188,6 +190,18 @@ class Api { return data; } + /** + * Perform a register attempt with the given credentials + * + * @param {object} credentials The credentials needed for login + * e.g. {email: 'foo@bar.com', password: 'secret', companyName: 'pno'} + * @returns The API response + */ + async register(credentials) { + const { data } = await axios.post(this.registerEndpoint, credentials); + return data; + } + /** * Refreshes both tokens * diff --git a/lib/middleware.js b/lib/middleware.js index e517cac..58b440d 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -27,9 +27,11 @@ Middleware.auth = function ({ store, redirect, route, $cookies }) { } // If the user is not authenticated + if ( !store.state.auth.user && route.path !== options.loginRoute && + route.path !== options.registerRoute && !isPublicRoute ) { return redirect(options.loginRoute); diff --git a/lib/module.js b/lib/module.js index 5183acb..7078044 100644 --- a/lib/module.js +++ b/lib/module.js @@ -6,8 +6,10 @@ export default function (moduleOptions) { accessTokenCookieName: 'access_token', refreshTokenCookieName: 'refresh_token', loginRoute: '/login', + registerRoute: '/register', homeRoute: '/', accessTokenEndpoint: '/authentication_token', + registerEndpoint: '/register', refreshTokenEndpoint: '/token_refresh', hideLoginWhenAuthenticated: true, usersEntity: 'users', diff --git a/lib/plugin.js b/lib/plugin.js index f4d1a19..8af1d33 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -9,6 +9,7 @@ export default async (ctx, inject) => { Api.setOptions({ baseUrl: options.apiUrl, accessTokenEndpoint: options.accessTokenEndpoint, + registerEndpoint: options.registerEndpoint, refreshTokenEndpoint: options.refreshTokenEndpoint, mercureUrl: options.mercureUrl, usersEntity: options.usersEntity, @@ -135,6 +136,30 @@ class Auth { } } + /** + * Performs the login request and handles persistence of necessary tokens + * + * @param {object} credentials The credentials needed for login + * e.g. {email: 'foo@bar.com', password: 'secret'} + */ + async register(credentials) { + try { + await this.$api.register(credentials); + + this.$router.push(this.options.loginRoute); + } catch (error) { + console.log(error); + // if (error.message === '401') { + // const authError = new Error('AuthError'); + // authError.message = 'Authentication Failure'; + // authError.data = 'You entered invalid credentials'; + // throw authError; + // } else { + // throw new Error(error); + // } + } + } + /** * Performs a logout */ From 7a0de921ad4fe1161d96cf1656753eb7cccfbf83 Mon Sep 17 00:00:00 2001 From: Jan Wolfram Date: Tue, 13 Sep 2022 10:01:48 +0200 Subject: [PATCH 2/7] feat: avoid forwarding after register --- lib/plugin.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/plugin.js b/lib/plugin.js index 8af1d33..7f2bade 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -145,8 +145,6 @@ class Auth { async register(credentials) { try { await this.$api.register(credentials); - - this.$router.push(this.options.loginRoute); } catch (error) { console.log(error); // if (error.message === '401') { From aa783411284bbfb1c7811e492b117b3d3a6646f3 Mon Sep 17 00:00:00 2001 From: Jan Wolfram Date: Tue, 13 Sep 2022 17:24:41 +0200 Subject: [PATCH 3/7] feat: added confirm route --- lib/middleware.js | 2 +- lib/module.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/middleware.js b/lib/middleware.js index 58b440d..b446eb3 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -27,11 +27,11 @@ Middleware.auth = function ({ store, redirect, route, $cookies }) { } // If the user is not authenticated - if ( !store.state.auth.user && route.path !== options.loginRoute && route.path !== options.registerRoute && + route.path !== options.confirmRoute && !isPublicRoute ) { return redirect(options.loginRoute); diff --git a/lib/module.js b/lib/module.js index 7078044..c25879b 100644 --- a/lib/module.js +++ b/lib/module.js @@ -6,6 +6,7 @@ export default function (moduleOptions) { accessTokenCookieName: 'access_token', refreshTokenCookieName: 'refresh_token', loginRoute: '/login', + confirmRoute: '/confirm', registerRoute: '/register', homeRoute: '/', accessTokenEndpoint: '/authentication_token', From b93445cfc0c69f70b9955a6b970f5e3d745cd5df Mon Sep 17 00:00:00 2001 From: Jan Wolfram Date: Thu, 15 Sep 2022 08:58:50 +0200 Subject: [PATCH 4/7] feat: activation --- lib/api/index.js | 5 +++++ lib/middleware.js | 1 + lib/module.js | 1 + 3 files changed, 7 insertions(+) diff --git a/lib/api/index.js b/lib/api/index.js index 934373e..02c37bb 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -262,6 +262,11 @@ class Api { items(name) { return new Items(name, axios, this.minioOptions); } + + async activate(id) { + const { data } = await axios.get(`activate?id=${id}`); + return data; + } } export default new Api(); diff --git a/lib/middleware.js b/lib/middleware.js index b446eb3..3adf3c5 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -32,6 +32,7 @@ Middleware.auth = function ({ store, redirect, route, $cookies }) { route.path !== options.loginRoute && route.path !== options.registerRoute && route.path !== options.confirmRoute && + route.path !== options.activateRoute && !isPublicRoute ) { return redirect(options.loginRoute); diff --git a/lib/module.js b/lib/module.js index c25879b..3c644ce 100644 --- a/lib/module.js +++ b/lib/module.js @@ -8,6 +8,7 @@ export default function (moduleOptions) { loginRoute: '/login', confirmRoute: '/confirm', registerRoute: '/register', + activateRoute: '/activate', homeRoute: '/', accessTokenEndpoint: '/authentication_token', registerEndpoint: '/register', From 16e8c902c1c96b7bb0932495cc3366fc19a8dd0e Mon Sep 17 00:00:00 2001 From: Jan Wolfram Date: Thu, 15 Sep 2022 17:31:55 +0200 Subject: [PATCH 5/7] feat: registerAdmin % registeruser --- lib/api/index.js | 24 +++++++++++++++++++----- lib/module.js | 3 ++- lib/plugin.js | 31 +++++++++++++++++++++++++++---- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/lib/api/index.js b/lib/api/index.js index 02c37bb..9366511 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -40,7 +40,8 @@ class Api { baseUrl = null, accessTokenEndpoint = null, refreshTokenEndpoint = null, - registerEndpoint = null, + registerAdminEndpoint = null, + registerUserEndpoint = null, mercureUrl = null, usersEntity = null, accessTokenUserIdKey = null, @@ -53,7 +54,8 @@ class Api { this.jwt = jwt; this.baseUrl = baseUrl; this.accessTokenEndpoint = accessTokenEndpoint; - this.registerEndpoint = registerEndpoint; + this.registerAdminEndpoint = registerAdminEndpoint; + this.registerUserEndpoint = registerUserEndpoint; this.refreshTokenEndpoint = refreshTokenEndpoint; this.mercureUrl = mercureUrl; this.eventSource = null; @@ -191,14 +193,26 @@ class Api { } /** - * Perform a register attempt with the given credentials + * Perform a register (ROLE_ADMIN) attempt with the given credentials * * @param {object} credentials The credentials needed for login * e.g. {email: 'foo@bar.com', password: 'secret', companyName: 'pno'} * @returns The API response */ - async register(credentials) { - const { data } = await axios.post(this.registerEndpoint, credentials); + async registerAdmin(credentials) { + const { data } = await axios.post(this.registerAdminEndpoint, credentials); + return data; + } + + /** + * Perform a register (ROLE_USER) attempt with the given credentials + * + * @param {object} credentials The credentials needed for login + * e.g. {email: 'foo@bar.com', password: 'secret', companyName: 'pno'} + * @returns The API response + */ + async registerUser(credentials) { + const { data } = await axios.post(this.registerUserEndpoint, credentials); return data; } diff --git a/lib/module.js b/lib/module.js index 3c644ce..f67dd80 100644 --- a/lib/module.js +++ b/lib/module.js @@ -11,7 +11,8 @@ export default function (moduleOptions) { activateRoute: '/activate', homeRoute: '/', accessTokenEndpoint: '/authentication_token', - registerEndpoint: '/register', + registerAdminEndpoint: '/register_admin', + registerUserEndpoint: '/register_user', refreshTokenEndpoint: '/token_refresh', hideLoginWhenAuthenticated: true, usersEntity: 'users', diff --git a/lib/plugin.js b/lib/plugin.js index 7f2bade..2c4da8d 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -9,7 +9,8 @@ export default async (ctx, inject) => { Api.setOptions({ baseUrl: options.apiUrl, accessTokenEndpoint: options.accessTokenEndpoint, - registerEndpoint: options.registerEndpoint, + registerAdminEndpoint: options.registerAdminEndpoint, + registerUserEndpoint: options.registerUserEndpoint, refreshTokenEndpoint: options.refreshTokenEndpoint, mercureUrl: options.mercureUrl, usersEntity: options.usersEntity, @@ -137,14 +138,36 @@ class Auth { } /** - * Performs the login request and handles persistence of necessary tokens + * Performs the register request of an admin and handles persistence of necessary tokens + * + * @param {object} credentials The credentials needed for login + * e.g. {email: 'foo@bar.com', password: 'secret'} + */ + async registerAdmin(credentials) { + try { + await this.$api.registerAdmin(credentials); + } catch (error) { + console.log(error); + // if (error.message === '401') { + // const authError = new Error('AuthError'); + // authError.message = 'Authentication Failure'; + // authError.data = 'You entered invalid credentials'; + // throw authError; + // } else { + // throw new Error(error); + // } + } + } + + /** + * Performs the register of an normal User request of an admin and handles persistence of necessary tokens * * @param {object} credentials The credentials needed for login * e.g. {email: 'foo@bar.com', password: 'secret'} */ - async register(credentials) { + async registerUser(credentials) { try { - await this.$api.register(credentials); + await this.$api.registerUser(credentials); } catch (error) { console.log(error); // if (error.message === '401') { From b07e3ac98284dbd05eb32362526b7ae40597e34d Mon Sep 17 00:00:00 2001 From: Jan Wolfram Date: Mon, 19 Sep 2022 12:07:19 +0200 Subject: [PATCH 6/7] feat: changed admin to superadmin --- lib/api/index.js | 11 +++++++---- lib/module.js | 2 +- lib/plugin.js | 6 +++--- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/api/index.js b/lib/api/index.js index 9366511..1c2e171 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -40,7 +40,7 @@ class Api { baseUrl = null, accessTokenEndpoint = null, refreshTokenEndpoint = null, - registerAdminEndpoint = null, + registerSuperAdminEndpoint = null, registerUserEndpoint = null, mercureUrl = null, usersEntity = null, @@ -54,7 +54,7 @@ class Api { this.jwt = jwt; this.baseUrl = baseUrl; this.accessTokenEndpoint = accessTokenEndpoint; - this.registerAdminEndpoint = registerAdminEndpoint; + this.registerSuperAdminEndpoint = registerSuperAdminEndpoint; this.registerUserEndpoint = registerUserEndpoint; this.refreshTokenEndpoint = refreshTokenEndpoint; this.mercureUrl = mercureUrl; @@ -199,8 +199,11 @@ class Api { * e.g. {email: 'foo@bar.com', password: 'secret', companyName: 'pno'} * @returns The API response */ - async registerAdmin(credentials) { - const { data } = await axios.post(this.registerAdminEndpoint, credentials); + async registerSuperAdmin(credentials) { + const { data } = await axios.post( + this.registerSuperAdminEndpoint, + credentials + ); return data; } diff --git a/lib/module.js b/lib/module.js index f67dd80..345d88f 100644 --- a/lib/module.js +++ b/lib/module.js @@ -11,7 +11,7 @@ export default function (moduleOptions) { activateRoute: '/activate', homeRoute: '/', accessTokenEndpoint: '/authentication_token', - registerAdminEndpoint: '/register_admin', + registerSuperAdminEndpoint: '/register_super_admin', registerUserEndpoint: '/register_user', refreshTokenEndpoint: '/token_refresh', hideLoginWhenAuthenticated: true, diff --git a/lib/plugin.js b/lib/plugin.js index 2c4da8d..28314f4 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -9,7 +9,7 @@ export default async (ctx, inject) => { Api.setOptions({ baseUrl: options.apiUrl, accessTokenEndpoint: options.accessTokenEndpoint, - registerAdminEndpoint: options.registerAdminEndpoint, + registerSuperAdminEndpoint: options.registerSuperAdminEndpoint, registerUserEndpoint: options.registerUserEndpoint, refreshTokenEndpoint: options.refreshTokenEndpoint, mercureUrl: options.mercureUrl, @@ -143,9 +143,9 @@ class Auth { * @param {object} credentials The credentials needed for login * e.g. {email: 'foo@bar.com', password: 'secret'} */ - async registerAdmin(credentials) { + async registerSuperAdmin(credentials) { try { - await this.$api.registerAdmin(credentials); + await this.$api.registerSuperAdmin(credentials); } catch (error) { console.log(error); // if (error.message === '401') { From dd69beb20babe43f0265c2107f24097e6fcee53f Mon Sep 17 00:00:00 2001 From: Jan Wolfram Date: Tue, 25 Oct 2022 11:29:56 +0200 Subject: [PATCH 7/7] feat: modified activate function --- lib/api/index.js | 6 ++++-- lib/api/items.js | 1 - lib/module.js | 1 + lib/plugin.js | 28 ++++++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/api/index.js b/lib/api/index.js index 1c2e171..c150bba 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -280,8 +280,10 @@ class Api { return new Items(name, axios, this.minioOptions); } - async activate(id) { - const { data } = await axios.get(`activate?id=${id}`); + async activate(id, email) { + const { data } = await axios.get(`activate?id=${id}&email=${email}`); + this.jwt = data.token; + this.refreshToken = data.refresh_token; return data; } } diff --git a/lib/api/items.js b/lib/api/items.js index 73a8088..c1fa1a9 100644 --- a/lib/api/items.js +++ b/lib/api/items.js @@ -80,7 +80,6 @@ export default class Items { } qs += `page=${opts.page}`; } - if (opts.id) { // get specific entry const { data } = await this.axios.get( diff --git a/lib/module.js b/lib/module.js index 345d88f..4c6bfb3 100644 --- a/lib/module.js +++ b/lib/module.js @@ -11,6 +11,7 @@ export default function (moduleOptions) { activateRoute: '/activate', homeRoute: '/', accessTokenEndpoint: '/authentication_token', + activateTokenEndpoint: '/activate', registerSuperAdminEndpoint: '/register_super_admin', registerUserEndpoint: '/register_user', refreshTokenEndpoint: '/token_refresh', diff --git a/lib/plugin.js b/lib/plugin.js index 88b2330..9cf8685 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -146,6 +146,34 @@ class Auth { } } + /** + * Performs the activate request and handles persistence of necessary tokens + * + * @param {String} id Confirmation Id + * @param {String} email Email of User + */ + async activate(id, email) { + try { + const activateData = await this.$api.activate(id, email); + this.$cookies.set(this.options.accessTokenCookieName, activateData.token); + this.$cookies.set( + this.options.refreshTokenCookieName, + activateData.refresh_token + ); + const user = await this.$api.me(); + await this.$store.commit('auth/SET_USER', user); + this.refreshTimer = setTimeout(() => { + this.refresh(); + }, this._getTimeUntilRefreshNeeded(activateData.token)); + this.$router.push(this.options.homeRoute); + } catch (error) { + const unexpectedError = new Error('UnexpectedError'); + unexpectedError.message = 'Unexpected Failure'; + unexpectedError.data = 'An unexpected error ocurred'; + throw unexpectedError; + } + } + /** * Performs the register request of an admin and handles persistence of necessary tokens *