-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compatibility with Medusa 1.17 sessions (#99)
* compatible with 1.17 sessions * added support for bearer auth * appending access token to url params * added expires in functionality back * fixed errors * added docs * resolved pr comments * minor formatting
- Loading branch information
1 parent
78f55ca
commit 92ff54e
Showing
10 changed files
with
233 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 29 additions & 17 deletions
46
packages/medusa-plugin-auth/src/core/auth-callback-middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,44 @@ | ||
import { Request, Response } from 'express'; | ||
import { ConfigModule } from '@medusajs/medusa/dist/types/global'; | ||
import jwt from 'jsonwebtoken'; | ||
|
||
/** | ||
* Return the handler of the auth callback for an auth strategy. Once the auth is successful this callback | ||
* will be called. | ||
* @param domain | ||
* @param secret | ||
* @param expiresIn | ||
* @param successRedirectGetter | ||
* @param successAction | ||
*/ | ||
export function authCallbackMiddleware( | ||
domain: 'admin' | 'store', | ||
secret: string, | ||
expiresIn: number, | ||
successRedirectGetter: () => string | ||
successAction: (req: Request, res: Response) => void | ||
) { | ||
return (req, res) => { | ||
const sendToken = sendTokenFactory(domain, secret, expiresIn); | ||
sendToken(req, res); | ||
res.redirect(successRedirectGetter()); | ||
successAction(req, res); | ||
}; | ||
} | ||
|
||
export function sendTokenFactory(domain: 'admin' | 'store', secret: string, expiresIn: number) { | ||
export function signToken(domain: 'admin' | 'store', configModule: ConfigModule, user: any, expiresIn?: number) { | ||
if(domain === 'admin') { | ||
return jwt.sign( | ||
{ user_id: user.id, domain: 'admin' }, | ||
configModule.projectConfig.jwt_secret, | ||
{ | ||
expiresIn: expiresIn ?? '24h', | ||
} | ||
); | ||
} else { | ||
return jwt.sign( | ||
{ customer_id: user.id, domain: 'store' }, | ||
configModule.projectConfig.jwt_secret, | ||
{ | ||
expiresIn: expiresIn ?? '30d', | ||
} | ||
); | ||
} | ||
} | ||
|
||
export function authenticateSessionFactory(domain: 'admin' | 'store') { | ||
return (req, res) => { | ||
const tokenData = | ||
domain === 'admin' ? { userId: req.user.id, ...req.user } : { customer_id: req.user.id, ...req.user }; | ||
const token = jwt.sign(tokenData, secret, { expiresIn }); | ||
const sessionKey = domain === 'admin' ? 'jwt' : 'jwt_store'; | ||
req.session[sessionKey] = token; | ||
const sessionKey = domain === 'admin' ? 'user_id' : 'customer_id'; | ||
|
||
req.session[sessionKey] = req.user.id; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.