Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class Api {
baseUrl = null,
accessTokenEndpoint = null,
refreshTokenEndpoint = null,
registerSuperAdminEndpoint = null,
registerUserEndpoint = null,
mercureUrl = null,
usersEntity = null,
accessTokenUserIdKey = null,
Expand All @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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();
1 change: 0 additions & 1 deletion lib/api/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export default class Items {
}
qs += `page=${opts.page}`;
}

if (opts.id) {
// get specific entry
const { data } = await this.axios.get(
Expand Down
3 changes: 3 additions & 0 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
74 changes: 74 additions & 0 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
*/
Expand Down