diff --git a/lib/api/index.js b/lib/api/index.js index 646c60e..26e4371 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -43,6 +43,8 @@ class Api { baseUrl = null, accessTokenEndpoint = null, refreshTokenEndpoint = null, + registerSuperAdminEndpoint = null, + registerUserEndpoint = null, mercureUrl = null, usersEntity = null, accessTokenUserIdKey = null, @@ -55,6 +57,8 @@ class Api { this.jwt = jwt; this.baseUrl = baseUrl; this.accessTokenEndpoint = accessTokenEndpoint; + this.registerSuperAdminEndpoint = registerSuperAdminEndpoint; + this.registerUserEndpoint = registerUserEndpoint; this.refreshTokenEndpoint = refreshTokenEndpoint; this.mercureUrl = mercureUrl; this.eventSource = null; @@ -191,6 +195,33 @@ class Api { return data; } + /** + * 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 registerSuperAdmin(credentials) { + const { data } = await axios.post( + this.registerSuperAdminEndpoint, + 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; + } + /** * Refreshes both tokens * @@ -251,6 +282,13 @@ class Api { items(name) { return new Items(name, axios, this.minioOptions); } + + 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; + } } export default new Api(); 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/middleware.js b/lib/middleware.js index e517cac..3adf3c5 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -30,6 +30,9 @@ Middleware.auth = function ({ store, redirect, route, $cookies }) { if ( !store.state.auth.user && 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 5183acb..4c6bfb3 100644 --- a/lib/module.js +++ b/lib/module.js @@ -6,8 +6,14 @@ export default function (moduleOptions) { accessTokenCookieName: 'access_token', refreshTokenCookieName: 'refresh_token', loginRoute: '/login', + confirmRoute: '/confirm', + registerRoute: '/register', + activateRoute: '/activate', homeRoute: '/', accessTokenEndpoint: '/authentication_token', + activateTokenEndpoint: '/activate', + registerSuperAdminEndpoint: '/register_super_admin', + registerUserEndpoint: '/register_user', refreshTokenEndpoint: '/token_refresh', hideLoginWhenAuthenticated: true, usersEntity: 'users', diff --git a/lib/plugin.js b/lib/plugin.js index 033ad5d..5f96342 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -9,6 +9,8 @@ export default async (ctx, inject) => { Api.setOptions({ baseUrl: options.apiUrl, accessTokenEndpoint: options.accessTokenEndpoint, + registerSuperAdminEndpoint: options.registerSuperAdminEndpoint, + registerUserEndpoint: options.registerUserEndpoint, refreshTokenEndpoint: options.refreshTokenEndpoint, mercureUrl: options.mercureUrl, usersEntity: options.usersEntity, @@ -144,6 +146,78 @@ 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 + * + * @param {object} credentials The credentials needed for login + * e.g. {email: 'foo@bar.com', password: 'secret'} + */ + async registerSuperAdmin(credentials) { + try { + await this.$api.registerSuperAdmin(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 registerUser(credentials) { + try { + await this.$api.registerUser(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 a logout */