-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a rest example which makes use of graphql-modules
- Loading branch information
Showing
9 changed files
with
245 additions
and
32 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
24 changes: 24 additions & 0 deletions
24
examples/rest-express-typescript-without-modules/README.md
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,24 @@ | ||
# rest-express-typescript | ||
|
||
This example demonstrate how to use [accounts-js](https://github.com/accounts-js/accounts). | ||
|
||
You must have a mongodb server running before starting the server. | ||
|
||
## Setup example | ||
|
||
In order to be able to run this example on your machine you first need to do the following steps: | ||
|
||
- Clone the repository `git clone [email protected]:accounts-js/accounts.git` | ||
- Install project dependencies: `yarn install` | ||
- Compile the packages `yarn run compile` | ||
- Go to the example folder `cd examples/rest-express-typescript` | ||
|
||
## Getting Started | ||
|
||
Start the app. | ||
|
||
``` | ||
yarn run start | ||
``` | ||
|
||
Open a browser and navigate to [http://localhost:4000](http://localhost:4000). |
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,19 @@ | ||
{ | ||
"version": 2, | ||
"name": "rest-express-typescript", | ||
"builds": [ | ||
{ | ||
"src": "lib/index.js", | ||
"use": "@now/node-server" | ||
} | ||
], | ||
"routes": [ | ||
{ | ||
"src": ".*", | ||
"dest": "/lib/index.js" | ||
} | ||
], | ||
"env": { | ||
"MONGO_URL": "@accounts-js-rest-express" | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
examples/rest-express-typescript-without-modules/package.json
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,23 @@ | ||
{ | ||
"name": "@examples/rest-express-typescript-without-modules", | ||
"private": true, | ||
"version": "0.32.0", | ||
"main": "lib/index.js", | ||
"license": "MIT", | ||
"scripts": { | ||
"start": "NODE_ENV=development yarn run -T nodemon -w src -x ts-node src/index.ts", | ||
"build": "yarn run -T tsc", | ||
"test": "yarn run build" | ||
}, | ||
"dependencies": { | ||
"@accounts/mongo": "^0.34.1", | ||
"@accounts/password": "^0.32.2", | ||
"@accounts/rest-express": "^0.33.1", | ||
"@accounts/server": "^0.33.1", | ||
"body-parser": "1.20.2", | ||
"cors": "2.8.5", | ||
"express": "4.18.2", | ||
"mongoose": "8.0.1", | ||
"tslib": "2.6.2" | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
examples/rest-express-typescript-without-modules/src/index.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,92 @@ | ||
import 'reflect-metadata'; | ||
import express from 'express'; | ||
import bodyParser from 'body-parser'; | ||
import cors from 'cors'; | ||
import mongoose from 'mongoose'; | ||
import { AccountsServer, ServerHooks } from '@accounts/server'; | ||
import { AccountsPassword } from '@accounts/password'; | ||
import accountsExpress, { userLoader } from '@accounts/rest-express'; | ||
import { Mongo } from '@accounts/mongo'; | ||
|
||
mongoose.connect(process.env.MONGO_URL || 'mongodb://localhost:27017/accounts-js-rest-example'); | ||
const db = mongoose.connection; | ||
|
||
const app = express(); | ||
|
||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
app.use(cors()); | ||
|
||
interface UserDoc extends mongoose.Document { | ||
firstName: string; | ||
lastName: string; | ||
} | ||
|
||
const User = mongoose.model<UserDoc>( | ||
'User', | ||
new mongoose.Schema({ firstName: String, lastName: String }) | ||
); | ||
|
||
const accountsPassword = new AccountsPassword({ | ||
// This option is called when a new user create an account | ||
// Inside we can apply our logic to validate the user fields | ||
validateNewUser: (user) => { | ||
// For example we can allow only some kind of emails | ||
if (user.email.endsWith('.xyz')) { | ||
throw new Error('Invalid email'); | ||
} | ||
return user; | ||
}, | ||
}); | ||
|
||
const accountsServer = new AccountsServer( | ||
{ | ||
tokenSecret: 'secret', | ||
}, | ||
{ | ||
password: accountsPassword, | ||
}, | ||
new Mongo(db) | ||
); | ||
|
||
accountsServer.on(ServerHooks.ValidateLogin, ({ user }) => { | ||
// This hook is called every time a user try to login. | ||
// You can use it to only allow users with verified email to login. | ||
// If you throw an error here it will be returned to the client. | ||
console.log('Logged in', user); | ||
}); | ||
|
||
/** | ||
* Load and expose the accounts-js middleware | ||
*/ | ||
app.use(accountsExpress(accountsServer)); | ||
|
||
/** | ||
* Return the current logged in user | ||
*/ | ||
app.get('/user', userLoader(accountsServer), (req, res) => { | ||
res.json({ user: (req as any).user }); | ||
}); | ||
|
||
/** | ||
* Expose a public route to edit user informations | ||
* - route is protected | ||
* - update the current logged in user in the db | ||
*/ | ||
app.put('/user', userLoader(accountsServer), async (req, res) => { | ||
const userId = (req as any).userId; | ||
if (!userId) { | ||
res.status(401); | ||
res.json({ message: 'Unauthorized' }); | ||
return; | ||
} | ||
const user = await User.findById(userId).exec(); | ||
user.firstName = req.body.firstName; | ||
user.lastName = req.body.lastName; | ||
await user.save(); | ||
res.json(true); | ||
}); | ||
|
||
app.listen(process.env.PORT || 4000, () => { | ||
console.log('Server listening on port 4000'); | ||
}); |
13 changes: 13 additions & 0 deletions
13
examples/rest-express-typescript-without-modules/tsconfig.json
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,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"outDir": "./lib", | ||
"target": "es5", | ||
"lib": ["es2015", "esnext.asynciterable"], | ||
"sourceMap": true, | ||
"importHelpers": true, | ||
"skipLibCheck": true, | ||
"isolatedModules": true, | ||
"esModuleInterop": true | ||
}, | ||
"include": ["./src/**/*"] | ||
} |
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