parent
dab38cbc18
commit
7786533a90
@ -1,15 +1,36 @@
|
|||||||
import Cookies from 'js-cookie';
|
import Cookies from 'js-cookie';
|
||||||
|
import jwtDecode from 'jwt-decode';
|
||||||
|
|
||||||
import Config from '../constants/Config';
|
import Config from '../constants/Config';
|
||||||
|
|
||||||
export const setAccessToken = (accessToken) => {
|
export const setAccessToken = (accessToken) => {
|
||||||
|
const { exp } = jwtDecode(accessToken);
|
||||||
|
const expires = new Date(exp * 1000);
|
||||||
|
|
||||||
Cookies.set(Config.ACCESS_TOKEN_KEY, accessToken, {
|
Cookies.set(Config.ACCESS_TOKEN_KEY, accessToken, {
|
||||||
expires: Config.ACCESS_TOKEN_EXPIRES,
|
expires,
|
||||||
|
secure: window.location.protocol === 'https:',
|
||||||
|
sameSite: 'strict',
|
||||||
});
|
});
|
||||||
};
|
|
||||||
|
|
||||||
export const getAccessToken = () => Cookies.get(Config.ACCESS_TOKEN_KEY);
|
Cookies.set(Config.ACCESS_TOKEN_VERSION_KEY, Config.ACCESS_TOKEN_VERSION, {
|
||||||
|
expires,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
export const removeAccessToken = () => {
|
export const removeAccessToken = () => {
|
||||||
Cookies.remove(Config.ACCESS_TOKEN_KEY);
|
Cookies.remove(Config.ACCESS_TOKEN_KEY);
|
||||||
|
Cookies.remove(Config.ACCESS_TOKEN_VERSION_KEY);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getAccessToken = () => {
|
||||||
|
let accessToken = Cookies.get(Config.ACCESS_TOKEN_KEY);
|
||||||
|
const accessTokenVersion = Cookies.get(Config.ACCESS_TOKEN_VERSION_KEY);
|
||||||
|
|
||||||
|
if (accessToken && accessTokenVersion !== Config.ACCESS_TOKEN_VERSION) {
|
||||||
|
removeAccessToken();
|
||||||
|
accessToken = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return accessToken;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,4 +1,9 @@
|
|||||||
TZ=UTC
|
|
||||||
BASE_URL=http://localhost:1337
|
BASE_URL=http://localhost:1337
|
||||||
DATABASE_URL=postgresql://postgres@localhost/planka
|
DATABASE_URL=postgresql://postgres@localhost/planka
|
||||||
SECRET_KEY=notsecretkey
|
SECRET_KEY=notsecretkey
|
||||||
|
|
||||||
|
# In days
|
||||||
|
TOKEN_EXPIRES_IN=365
|
||||||
|
|
||||||
|
# Do not edit this
|
||||||
|
TZ=UTC
|
||||||
|
|||||||
@ -0,0 +1,29 @@
|
|||||||
|
const jwt = require('jsonwebtoken');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
sync: true,
|
||||||
|
|
||||||
|
inputs: {
|
||||||
|
subject: {
|
||||||
|
type: 'json',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
issuedAt: {
|
||||||
|
type: 'ref',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
fn(inputs) {
|
||||||
|
const { issuedAt = new Date() } = inputs;
|
||||||
|
const iat = Math.floor(issuedAt / 1000);
|
||||||
|
|
||||||
|
return jwt.sign(
|
||||||
|
{
|
||||||
|
iat,
|
||||||
|
sub: inputs.subject,
|
||||||
|
exp: iat + sails.config.custom.tokenExpiresIn * 24 * 60 * 60,
|
||||||
|
},
|
||||||
|
sails.config.session.secret,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
};
|
||||||
@ -1,16 +0,0 @@
|
|||||||
const jwt = require('jsonwebtoken');
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
sync: true,
|
|
||||||
|
|
||||||
inputs: {
|
|
||||||
payload: {
|
|
||||||
type: 'json',
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
fn(inputs) {
|
|
||||||
return jwt.sign(inputs.payload, sails.config.session.secret);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
module.exports.up = async (knex) =>
|
||||||
|
knex.schema.table('user_account', (table) => {
|
||||||
|
/* Columns */
|
||||||
|
|
||||||
|
table.timestamp('password_changed_at', true);
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports.down = async (knex) =>
|
||||||
|
knex.schema.table('user_account', (table) => {
|
||||||
|
table.dropColumn('password_changed_at');
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue