mirror of
https://github.com/umami-software/umami.git
synced 2026-02-20 20:45:39 +01:00
auth flow
This commit is contained in:
parent
784237bfab
commit
5d43ef8540
2 changed files with 53 additions and 4 deletions
|
|
@ -94,6 +94,7 @@
|
|||
"maxmind": "^4.3.6",
|
||||
"moment-timezone": "^0.5.35",
|
||||
"next": "14.0.4",
|
||||
"next-auth": "^4.24.5",
|
||||
"next-basics": "^0.39.0",
|
||||
"node-fetch": "^3.2.8",
|
||||
"npm-run-all": "^4.1.5",
|
||||
|
|
|
|||
|
|
@ -15,6 +15,48 @@ import {
|
|||
} from 'next-basics';
|
||||
import { NextApiRequestCollect } from 'pages/api/send';
|
||||
import { getUserById } from '../queries';
|
||||
import NextAuth from "next-auth"
|
||||
import CognitoProvider from "next-auth/providers/cognito";
|
||||
import { to } from '@react-spring/web';
|
||||
|
||||
|
||||
export const authOptions = {
|
||||
providers: [
|
||||
CognitoProvider({
|
||||
clientId: process.env.COGNITO_CLIENT_ID,
|
||||
clientSecret: process.env.COGNITO_CLIENT_SECRET ,
|
||||
issuer: process.env.COGNITO_DOMAIN ,
|
||||
idToken: true,
|
||||
name: 'Cognito',
|
||||
checks: 'nonce',
|
||||
}),
|
||||
],
|
||||
callbacks: {
|
||||
async jwt({ token, user, account }) {
|
||||
console.log("in next auth::::",token)
|
||||
if (account) {
|
||||
if (account['provider'] === 'cognito') {
|
||||
var tokenParsed = JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString());
|
||||
console.log("token parsed",tokenParsed )
|
||||
// token.refreshToken = account?.refresh_token;
|
||||
// token.accessTokenExpires = account.expires_at * 1000;
|
||||
console.log("token parsed::::",tokenParsed['cognito:username'],tokenParsed['iat'])
|
||||
return { userId: tokenParsed['cognito:username'], iat: tokenParsed['iat'] };
|
||||
}
|
||||
}
|
||||
// Return previous token if the access token has not expired yet
|
||||
if ((Date.now()) < (token.accessTokenExpires ?? 0)) {
|
||||
return token;
|
||||
}
|
||||
|
||||
// Access token has expired, try to update it
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
export default NextAuth(authOptions)
|
||||
|
||||
const log = debug('umami:middleware');
|
||||
|
||||
|
|
@ -50,11 +92,18 @@ export const useSession = createMiddleware(async (req, res, next) => {
|
|||
|
||||
export const useAuth = createMiddleware(async (req, res, next) => {
|
||||
const token = getAuthToken(req);
|
||||
//console.log("got auth token",token)
|
||||
const payload = parseSecureToken(token, secret());
|
||||
const shareToken = await parseShareToken(req as any);
|
||||
|
||||
//console.log("got shareToken",shareToken);
|
||||
let cognitoPayload = {};
|
||||
if(!payload){
|
||||
cognitoPayload = await authOptions.callbacks.jwt({token:token,user:"",account:{provider:"cognito"}});
|
||||
}
|
||||
console.log("cognito auth payload",cognitoPayload)
|
||||
console.log("umami auth payload ",payload);
|
||||
let user = null;
|
||||
const { userId, authKey, grant } = payload || {};
|
||||
const { userId, authKey, grant } = payload || cognitoPayload || {};
|
||||
|
||||
if (userId) {
|
||||
user = await getUserById(userId);
|
||||
|
|
@ -94,7 +143,6 @@ export const useValidate = async (schema, req, res) => {
|
|||
return createMiddleware(async (req: any, res, next) => {
|
||||
try {
|
||||
const rules = schema[req.method];
|
||||
|
||||
if (rules) {
|
||||
rules.validateSync({ ...req.query, ...req.body });
|
||||
}
|
||||
|
|
@ -104,4 +152,4 @@ export const useValidate = async (schema, req, res) => {
|
|||
|
||||
next();
|
||||
})(req, res);
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue