-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(deps): update dependency @auth0/auth0-spa-js to v2 (#7524)
* chore(deps): update dependency @auth0/auth0-spa-js to v2 * fix type errors * changes to setup * add wip codemod * add upgrade step * finish codemod --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Dominic Saadi <[email protected]>
- Loading branch information
1 parent
4b00e5a
commit 37999cb
Showing
10 changed files
with
140 additions
and
55 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
22 changes: 22 additions & 0 deletions
22
packages/codemods/src/codemods/v5.x.x/updateAuth0/__testfixtures__/default.input.js
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Auth0Client } from '@auth0/auth0-spa-js' | ||
|
||
import { createAuth } from '@redwoodjs/auth-auth0-web' | ||
|
||
const auth0 = new Auth0Client({ | ||
domain: process.env.AUTH0_DOMAIN || '', | ||
client_id: process.env.AUTH0_CLIENT_ID || '', | ||
redirect_uri: process.env.AUTH0_REDIRECT_URI, | ||
|
||
// Storing tokens in the browser's local storage provides persistence across page refreshes and browser tabs. | ||
// But if an attacker can run JavaScript in your SPA using a cross-site scripting (XSS) attack, | ||
// they can retrieve the tokens stored in local storage. | ||
// See https://auth0.com/docs/libraries/auth0-spa-js#change-storage-options. | ||
cacheLocation: 'localstorage', | ||
audience: process.env.AUTH0_AUDIENCE, | ||
|
||
// `useRefreshTokens` is required for automatically extending sessions beyond what's set in the initial JWT expiration. | ||
// See https://auth0.com/docs/tokens/refresh-tokens. | ||
// useRefreshTokens: true, | ||
}) | ||
|
||
export const { AuthProvider, useAuth } = createAuth(auth0) |
21 changes: 21 additions & 0 deletions
21
packages/codemods/src/codemods/v5.x.x/updateAuth0/__testfixtures__/default.output.js
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Auth0Client } from '@auth0/auth0-spa-js' | ||
|
||
import { createAuth } from '@redwoodjs/auth-auth0-web' | ||
|
||
const auth0 = new Auth0Client({ | ||
domain: process.env.AUTH0_DOMAIN || '', | ||
clientId: process.env.AUTH0_CLIENT_ID || '', | ||
|
||
authorizationParams: { | ||
redirect_uri: process.env.AUTH0_REDIRECT_URI, | ||
audience: process.env.AUTH0_AUDIENCE, | ||
}, | ||
|
||
// Storing tokens in the browser's local storage provides persistence across page refreshes and browser tabs. | ||
// But if an attacker can run JavaScript in your SPA using a cross-site scripting (XSS) attack, | ||
// they can retrieve the tokens stored in local storage. | ||
// See https://auth0.com/docs/libraries/auth0-spa-js#change-storage-options. | ||
cacheLocation: 'localstorage', | ||
}) | ||
|
||
export const { AuthProvider, useAuth } = createAuth(auth0) |
5 changes: 5 additions & 0 deletions
5
packages/codemods/src/codemods/v5.x.x/updateAuth0/__tests__/updateAuth0.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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
describe('auth0', () => { | ||
it('updates the auth0 file', async () => { | ||
await matchTransformSnapshot('updateAuth0', 'default') | ||
}) | ||
}) |
36 changes: 36 additions & 0 deletions
36
packages/codemods/src/codemods/v5.x.x/updateAuth0/updateAuth0.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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { FileInfo, API } from 'jscodeshift' | ||
|
||
export default function transform(file: FileInfo, api: API) { | ||
const j = api.jscodeshift | ||
const ast = j(file.source) | ||
|
||
const paths = ast.find(j.ObjectProperty, (node) => { | ||
return ['redirect_uri', 'audience'].includes(node.key.name) | ||
}) | ||
|
||
let nodes = paths.nodes() | ||
|
||
nodes = nodes.map((node) => { | ||
const { comments: _comments, ...rest } = node | ||
return rest | ||
}) | ||
|
||
paths.remove() | ||
|
||
ast | ||
.find(j.ObjectProperty, { key: { name: 'client_id' } }) | ||
.insertAfter( | ||
j.objectProperty( | ||
j.identifier('authorizationParams'), | ||
j.objectExpression(nodes) | ||
) | ||
) | ||
|
||
ast.find(j.Identifier, { name: 'client_id' }).replaceWith('clientId') | ||
|
||
return ast.toSource({ | ||
trailingComma: true, | ||
quote: 'single', | ||
lineTerminator: '\n', | ||
}) | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/codemods/src/codemods/v5.x.x/updateAuth0/updateAuth0.yargs.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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import path from 'path' | ||
|
||
import execa from 'execa' | ||
import task, { TaskInnerAPI } from 'tasuku' | ||
|
||
import getRWPaths from '../../../lib/getRWPaths' | ||
import isTSProject from '../../../lib/isTSProject' | ||
import runTransform from '../../../lib/runTransform' | ||
|
||
export const command = 'update-auth0' | ||
export const description = | ||
'(v4.x.x->v5.x.x) For Auth0 users; updates the web-side auth.ts,js file' | ||
|
||
export const handler = () => { | ||
task('Update Auth0', async ({ setOutput }: TaskInnerAPI) => { | ||
const authFile = isTSProject ? 'auth.ts' : 'auth.js' | ||
|
||
try { | ||
await execa.command('yarn up @auth0/auth0-spa-js@^2', { | ||
cwd: getRWPaths().web.base, | ||
}) | ||
} catch { | ||
console.error( | ||
"Couldn't update @auth0/auth0-spa-js; you'll have to upgrade it manually to the latest v2.x.x version" | ||
) | ||
} | ||
|
||
await runTransform({ | ||
transformPath: path.join(__dirname, 'updateAuth0.js'), | ||
targetPaths: [path.join(getRWPaths().web.src, authFile)], | ||
}) | ||
|
||
setOutput('All done! Run `yarn rw lint --fix` to prettify your code') | ||
}) | ||
} |
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