Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OAuth2 example apps #131

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ buck-out/
\.buckd/
*.keystore

!lib/build/
!lib/build/

.expo
42 changes: 42 additions & 0 deletions examples/st-oauth2-authorization-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
![SuperTokens banner](https://raw.githubusercontent.com/supertokens/supertokens-logo/master/images/Artboard%20%E2%80%93%2027%402x.png)

# SuperTokens OAuth2 Authorization Server

This example app uses SuperTokens `OAuth2Provider` recipe to expose OAuth2 APIs. This app acts as an OAuth2 authorization server for other OAuth2 examples in this repo.

## Project setup

Clone the repo, enter the directory, and use `npm` to install the project dependencies:

```bash
git clone https://github.com/supertokens/supertokens-react-native
cd supertokens-react-native/examples/st-oauth2-authorization-server
npm install
```

## Set Up Frontend and Backend URLs

By default, the frontend runs at `http://localhost:3005`, and the backend at `http://localhost:3006`. You can customize these by setting the `REACT_APP_AUTH_SERVER_WEBSITE_URL` and `REACT_APP_AUTH_SERVER_API_URL` environment variables.

When running locally, we recommend using your local IP address as the domain to easily access these APIs from an emulator. On Mac/Linux, you can find your local IP with `ifconfig getifaddr en0`. If your IP is `10.64.21.128`, set the environment variables as follows:

```bash
export REACT_APP_AUTH_SERVER_WEBSITE_URL="http://10.64.21.128:3005"
export REACT_APP_AUTH_SERVER_API_URL="http://10.64.21.128:3006"
```

## Run the demo app

This compiles and serves the React app and starts the backend API server.

```bash
npm run start
```

## Author

Created with :heart: by the folks at supertokens.com.

## License

This project is licensed under the Apache 2.0 license.
27 changes: 27 additions & 0 deletions examples/st-oauth2-authorization-server/backend/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import EmailPassword from "supertokens-node/recipe/emailpassword";
import OAuth2Provider from "supertokens-node/recipe/oauth2provider";
import Session from "supertokens-node/recipe/session";
import { TypeInput } from "supertokens-node/types";

export function getWebsiteDomain() {
return process.env.REACT_APP_AUTH_SERVER_WEBSITE_URL || "http://localhost:3005";
}

export function getApiDomain() {
return process.env.REACT_APP_AUTH_SERVER_API_URL || "http://localhost:3006";
}

export const SuperTokensConfig: TypeInput = {
supertokens: {
// this is the location of the SuperTokens core.
connectionURI: "https://try.supertokens.com",
},
appInfo: {
appName: "SuperTokens Demo App",
apiDomain: getApiDomain(),
websiteDomain: getWebsiteDomain(),
},
// recipeList contains all the modules that you want to
// use from SuperTokens. See the full list here: https://supertokens.com/docs/guides
recipeList: [EmailPassword.init(), OAuth2Provider.init(), Session.init()],
};
49 changes: 49 additions & 0 deletions examples/st-oauth2-authorization-server/backend/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import express from "express";
import cors from "cors";
import supertokens from "supertokens-node";
import { verifySession } from "supertokens-node/recipe/session/framework/express";
import { middleware, errorHandler, SessionRequest } from "supertokens-node/framework/express";
import { getApiDomain, getWebsiteDomain, SuperTokensConfig } from "./config";
import Multitenancy from "supertokens-node/recipe/multitenancy";

supertokens.init(SuperTokensConfig);

const app = express();

app.use(
cors({
origin: [getWebsiteDomain(), "http://localhost:3000"],
// origin: [getWebsiteDomain()],
allowedHeaders: ["content-type", ...supertokens.getAllCORSHeaders()],
methods: ["GET", "PUT", "POST", "DELETE"],
credentials: true,
})
);

// This exposes all the APIs from SuperTokens to the client.
app.use(middleware());

// An example API that requires session verification
app.get("/sessioninfo", verifySession(), async (req: SessionRequest, res) => {
let session = req.session;
res.send({
sessionHandle: session!.getHandle(),
userId: session!.getUserId(),
accessTokenPayload: session!.getAccessTokenPayload(),
});
});

// This API is used by the frontend to create the tenants drop down when the app loads.
// Depending on your UX, you can remove this API.
app.get("/tenants", async (req, res) => {
let tenants = await Multitenancy.listAllTenants();
res.send(tenants);
});

// In case of session related errors, this error handler
// returns 401 to the client.
app.use(errorHandler());

const PORT = process.env.PORT || 3006;

app.listen(PORT, () => console.log(`API Server listening on ${getApiDomain()}`));
Loading
Loading