Skip to content

Commit 711d18f

Browse files
authored
fix: preserve url fragment when redirectBack is used (#792)
* fix: preserve url fragment when redirectBack is used * fix: PR changes * fix: PR changes * fix: PR changes * fix: Add with-hash-router example
1 parent 4e4ac3e commit 711d18f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1505
-42
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
77

8+
## [0.38.0] - 2024-02-29
9+
10+
## Breaking Changes
11+
12+
- Previously, when calling `redirectToAuth` with the `redirectBack` option, url fragments(hash) were stripped when redirecting back to the previous page after authentication. This issue has been fixed, and url fragments are preserved along with the query params. For users who employed the workaround of overriding the `windowHandler.location.getSearch` function to include fragments, it is essential to remove that override before upgrading to this version.
13+
814
## [0.37.1] - 2024-02-26
915

1016
- Fixes types argument in override functions to make `builder` non optional.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
package-lock.json
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
![SuperTokens banner](https://raw.githubusercontent.com/supertokens/supertokens-logo/master/images/Artboard%20%E2%80%93%2027%402x.png)
2+
3+
# SuperTokens Demo app - with-hash-router
4+
5+
This demo app shows you how to use SuperTokens with a frontend router that uses hash routing. This example in particular uses [react-router-dom](https://reactrouter.com/web/guides/quick-start) with hash router.
6+
7+
The main idea is that we provide a custom `windowHandler` on the frontend's supertokens.init which will provide functions for getting the pathname, search and hash from the url.
8+
9+
You can find the implementation of this custom handler in `/frontend/src/windowHandler.ts`. The function `getWindowHandler` that is exposed by this file is used in `supertokens.init` in `/frontend/src/config.ts`.
10+
11+
You can start the demo app by running:
12+
13+
```bash
14+
git clone https://github.com/supertokens/supertokens-auth-react
15+
cd supertokens-auth-react/examples/with-hash-router
16+
17+
npm install
18+
19+
npm run start
20+
```
21+
22+
## Author
23+
24+
Created with :heart: by the folks at supertokens.com.
25+
26+
## License
27+
28+
This project is licensed under the Apache 2.0 license.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import EmailPassword from "supertokens-node/recipe/emailpassword";
2+
import Session from "supertokens-node/recipe/session";
3+
import { TypeInput } from "supertokens-node/types";
4+
import Dashboard from "supertokens-node/recipe/dashboard";
5+
import EmailVerification from "supertokens-node/recipe/emailverification";
6+
7+
export function getApiDomain() {
8+
const apiPort = process.env.REACT_APP_API_PORT || 3001;
9+
const apiUrl = process.env.REACT_APP_API_URL || `http://localhost:${apiPort}`;
10+
return apiUrl;
11+
}
12+
13+
export function getWebsiteDomain() {
14+
const websitePort = process.env.REACT_APP_WEBSITE_PORT || 3000;
15+
const websiteUrl = process.env.REACT_APP_WEBSITE_URL || `http://localhost:${websitePort}`;
16+
return websiteUrl;
17+
}
18+
19+
export const SuperTokensConfig: TypeInput = {
20+
supertokens: {
21+
// this is the location of the SuperTokens core.
22+
connectionURI: "https://try.supertokens.com",
23+
},
24+
appInfo: {
25+
appName: "SuperTokens Demo App",
26+
apiDomain: getApiDomain(),
27+
websiteDomain: getWebsiteDomain(),
28+
},
29+
// recipeList contains all the modules that you want to
30+
// use from SuperTokens. See the full list here: https://supertokens.com/docs/guides
31+
recipeList: [
32+
EmailPassword.init(),
33+
Session.init(),
34+
Dashboard.init(),
35+
EmailVerification.init({
36+
mode: "REQUIRED",
37+
}),
38+
],
39+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import express from "express";
2+
import cors from "cors";
3+
import supertokens from "supertokens-node";
4+
import { verifySession } from "supertokens-node/recipe/session/framework/express";
5+
import { middleware, errorHandler, SessionRequest } from "supertokens-node/framework/express";
6+
import { getWebsiteDomain, SuperTokensConfig } from "./config";
7+
8+
supertokens.init(SuperTokensConfig);
9+
10+
const app = express();
11+
12+
app.use(
13+
cors({
14+
origin: getWebsiteDomain(),
15+
allowedHeaders: ["content-type", ...supertokens.getAllCORSHeaders()],
16+
methods: ["GET", "PUT", "POST", "DELETE"],
17+
credentials: true,
18+
})
19+
);
20+
21+
// This exposes all the APIs from SuperTokens to the client.
22+
app.use(middleware());
23+
24+
// An example API that requires session verification
25+
app.get("/sessioninfo", verifySession(), async (req: SessionRequest, res) => {
26+
let session = req.session;
27+
res.send({
28+
sessionHandle: session!.getHandle(),
29+
userId: session!.getUserId(),
30+
accessTokenPayload: session!.getAccessTokenPayload(),
31+
});
32+
});
33+
34+
// In case of session related errors, this error handler
35+
// returns 401 to the client.
36+
app.use(errorHandler());
37+
38+
app.listen(3001, () => console.log(`API Server listening on port 3001`));
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "supertokens-node",
3+
"version": "0.0.1",
4+
"private": true,
5+
"description": "",
6+
"main": "index.js",
7+
"scripts": {
8+
"start": "npx ts-node-dev --project ./tsconfig.json ./index.ts"
9+
},
10+
"dependencies": {},
11+
"devDependencies": {
12+
"@types/cors": "^2.8.12",
13+
"@types/morgan": "^1.9.3",
14+
"@types/node": "^16.11.38",
15+
"nodemon": "^2.0.16"
16+
},
17+
"keywords": [],
18+
"author": "",
19+
"license": "ISC"
20+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"compilerOptions": {
3+
/* Visit https://aka.ms/tsconfig.json to read more about this file */
4+
/* Basic Options */
5+
// "incremental": true, /* Enable incremental compilation */
6+
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
7+
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
8+
// "lib": [], /* Specify library files to be included in the compilation. */
9+
// "allowJs": true, /* Allow javascript files to be compiled. */
10+
// "checkJs": true, /* Report errors in .js files. */
11+
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
12+
// "declaration": true, /* Generates corresponding '.d.ts' file. */
13+
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
14+
// "sourceMap": true, /* Generates corresponding '.map' file. */
15+
// "outFile": "./", /* Concatenate and emit output to single file. */
16+
// "outDir": "./", /* Redirect output structure to the directory. */
17+
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
18+
// "composite": true, /* Enable project compilation */
19+
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
20+
// "removeComments": true, /* Do not emit comments to output. */
21+
// "noEmit": true, /* Do not emit outputs. */
22+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
23+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
24+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
25+
/* Strict Type-Checking Options */
26+
"strict": true /* Enable all strict type-checking options. */,
27+
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
28+
// "strictNullChecks": true, /* Enable strict null checks. */
29+
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
30+
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
31+
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
32+
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
33+
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
34+
/* Additional Checks */
35+
// "noUnusedLocals": true, /* Report errors on unused locals. */
36+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
37+
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
38+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
39+
/* Module Resolution Options */
40+
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
41+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
42+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
43+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
44+
// "typeRoots": [], /* List of folders to include type definitions from. */
45+
// "types": [], /* Type declaration files to be included in compilation. */
46+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
47+
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
48+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
49+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
50+
/* Source Map Options */
51+
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
52+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
53+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
54+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
55+
/* Experimental Options */
56+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
57+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
58+
/* Advanced Options */
59+
"skipLibCheck": true /* Skip type checking of declaration files. */,
60+
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
61+
}
62+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SKIP_PREFLIGHT_CHECK=true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

0 commit comments

Comments
 (0)