|
| 1 | + |
| 2 | +# Using Passage for Login and Profile Pages |
| 3 | + |
| 4 | +In addition to login and regsitration functionality, Passage also provides a pre-built profile page that enables users to view and edit their public account data. This repo is an example of how to build a login and profile expereince using Passage in a Node.js web application built with Express. |
| 5 | + |
| 6 | +## Setup |
| 7 | + |
| 8 | +The [Passage Node.js SDK](https://www.npmjs.com/package/@passageidentity/passage-node) requires a configuration object. For this example app, we'll need to provide our application with a Passage App ID and API Key, which can be set in the `.env` file. Your App ID and API Key can be found in the [Passage Console](https://console.passage.id) in your App Settings. A sample `.env` file is shown here: |
| 9 | + |
| 10 | +``` |
| 11 | +PASSAGE_APP_ID= |
| 12 | +PASSAGE_API_KEY= |
| 13 | +PORT= |
| 14 | +``` |
| 15 | + |
| 16 | +## Run With Node |
| 17 | + |
| 18 | +To run this example app, make sure you have [node.js installed on your computer](https://nodejs.org/en/download/). |
| 19 | + |
| 20 | +To install the dependencies: |
| 21 | + |
| 22 | +```bash |
| 23 | + npm i |
| 24 | +``` |
| 25 | + |
| 26 | +To run the application in development mode: |
| 27 | + |
| 28 | +```bash |
| 29 | + npm run dev |
| 30 | +``` |
| 31 | + |
| 32 | +## Run With Docker |
| 33 | + |
| 34 | +Make sure you have [docker installed on your computer](https://docs.docker.com/get-docker/). |
| 35 | + |
| 36 | +Create your docker image: |
| 37 | + |
| 38 | +```bash |
| 39 | +$ docker build -t example-node-1 . |
| 40 | +``` |
| 41 | + |
| 42 | +Run your docker container using the example-node-1 image, changing the port numbers as necessary: |
| 43 | + |
| 44 | +```bash |
| 45 | +$ docker run -p 5000:5000 example-node-1 |
| 46 | +``` |
| 47 | + |
| 48 | +You can now visit http://localhost:5000 in your browser to see Passage in action. |
| 49 | + |
| 50 | +## Adding Authentication to the Frontend |
| 51 | + |
| 52 | +The easiest way to add authentication to a web frontend is with a Passage Element. The HTML below will automatically embed a complete UI/UX for user sign-in and sign-up. In this example application, we automatically use the PASSAGE_APP_ID environment variable in the `app-id` attribute. |
| 53 | + |
| 54 | +```html |
| 55 | +<!-- Passage will populate this div with a complete authentication UI/UX. --> |
| 56 | +<passage-auth app-id="<Passage App ID>"></passage-auth> |
| 57 | + |
| 58 | +<!-- Include the passage-web JavaScript from the Passage CDN. --> |
| 59 | +<script src="https://cdn.passage.id/passage-web.js"></script> |
| 60 | +``` |
| 61 | + |
| 62 | +## Authenticating an HTTP Request |
| 63 | + |
| 64 | +A Node.js Express server can easily authenticate an HTTP request using the Passage SDK, as shown below. Note that authenticating requests does not require an API key. |
| 65 | + |
| 66 | +```javascript |
| 67 | +import Passage from "@passageidentity/passage-node"; |
| 68 | +import express from "express"; |
| 69 | + |
| 70 | +const app = express(); |
| 71 | +const PORT = 5000; |
| 72 | + |
| 73 | +let passageConfig = { |
| 74 | + appID: "YOUR_APP_ID", |
| 75 | +}; |
| 76 | + |
| 77 | +// example of middleware using Passage |
| 78 | +let passage = new Passage(passageConfig); |
| 79 | +let passageAuthMiddleware = (() => { |
| 80 | + return async (req: any, res: any, next: NextFunction) => { |
| 81 | + try { |
| 82 | + let userID = await passage.authenticateRequest(req); |
| 83 | + if (userID) { |
| 84 | + // successfully authenticated. save user ID and continue |
| 85 | + res.userID = userID; |
| 86 | + next(); |
| 87 | + } |
| 88 | + } catch (e) { |
| 89 | + // failed authentication |
| 90 | + console.log(e); |
| 91 | + res.status(401).send("Could not authenticate user!"); |
| 92 | + } |
| 93 | + }; |
| 94 | +})(); |
| 95 | + |
| 96 | +// example usage of passage middleware |
| 97 | +app.get( |
| 98 | + "/authenticatedRoute", |
| 99 | + passageAuthMiddleware, |
| 100 | + async (req: Request, res: any) => { |
| 101 | + let userID = res.userID; |
| 102 | + // do authenticated things... |
| 103 | + } |
| 104 | +); |
| 105 | + |
| 106 | +app.listen(PORT, () => { |
| 107 | + console.log(`Example app running`); |
| 108 | +}); |
| 109 | +``` |
| 110 | + |
| 111 | +## User Management |
| 112 | + |
| 113 | +The example application also demonstrates some user management functionality, such as getting a user's information. These operations are sensitive and require an API Key to retrieve information from Passage. This can be generated in the [Passage Console](https://console.passage.id). |
| 114 | + |
| 115 | +```javascript |
| 116 | +// updated passage config |
| 117 | +let passageConfig = { |
| 118 | + appID: "YOUR_APP_ID", |
| 119 | + apiKey: "YOUR_API_KEY", |
| 120 | +}; |
| 121 | + |
| 122 | +// get user info after authenticating a route |
| 123 | +app.get( |
| 124 | + "/authenticatedRoute", |
| 125 | + passageAuthMiddleware, |
| 126 | + async (req: Request, res: any) => { |
| 127 | + let userID = res.userID; |
| 128 | + let user = passage.user.get(userID); |
| 129 | + console.log(user.email); |
| 130 | + } |
| 131 | +); |
| 132 | +``` |
| 133 | + |
0 commit comments