Skip to content

Commit

Permalink
fix: generate random password the first time (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p authored Nov 27, 2023
1 parent 03f4f1e commit 5f5f269
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ export function passportAuthRoutesBuilder({
if (options?.msg) {
if (passportCallbackAuthenticateMiddlewareOptions?.failureRedirect) {
return res.redirect(
passportCallbackAuthenticateMiddlewareOptions.failureRedirect + '?message=' + options.msg
passportCallbackAuthenticateMiddlewareOptions.failureRedirect +
'?message=' +
options.msg
);
} else {
return res.status(401).json({ message: options.msg });
Expand All @@ -115,7 +117,13 @@ export function passportAuthRoutesBuilder({
return router;
}

function successActionHandlerFactory(req: Request, domain: 'admin' | 'store', configModule: ConfigModule, defaultRedirect: string, expiresIn?: number) {
function successActionHandlerFactory(
req: Request,
domain: 'admin' | 'store',
configModule: ConfigModule,
defaultRedirect: string,
expiresIn?: number
) {
const returnAccessToken = req.query.returnAccessToken == 'true';
const redirectUrl = (req.query.redirectTo ? req.query.redirectTo : defaultRedirect) as string;

Expand All @@ -132,7 +140,6 @@ function successActionHandlerFactory(req: Request, domain: 'admin' | 'store', co

const token = signToken(domain, configModule, req.user, expiresIn);


// append token to redirect url as query param
const url = new URL(redirectUrl);
url.searchParams.append('access_token', token);
Expand Down
9 changes: 9 additions & 0 deletions packages/medusa-plugin-auth/src/core/validate-callback.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import crypto from 'crypto';
import { CustomerService, UserService } from '@medusajs/medusa';
import { MedusaContainer } from '@medusajs/medusa/dist/types/global';
import { MedusaError } from 'medusa-core-utils';
Expand Down Expand Up @@ -149,6 +150,13 @@ export async function validateStoreCallback<
}
}

const generatePassword = () => {
const characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!@-#$';
return Array.from(crypto.randomFillSync(new Uint32Array(20)))
.map((x) => characters[x % characters.length])
.join('');
};

customer = await customerService.withTransaction(transactionManager).create({
email,
metadata: {
Expand All @@ -159,6 +167,7 @@ export async function validateStoreCallback<
first_name: profile.name?.givenName ?? '',
last_name: profile.name?.familyName ?? '',
has_account: true,
password: generatePassword(),
});

return { id: customer.id };
Expand Down

1 comment on commit 5f5f269

@vercel
Copy link

@vercel vercel bot commented on 5f5f269 Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

medusa-plugins – ./

medusa-plugins-git-main-adrien2p.vercel.app
medusa-plugins.vercel.app
medusa-plugins-adrien2p.vercel.app

Please sign in to comment.