Skip to content

Commit 0c452a8

Browse files
committed
Add Sequelize example
Fixes #29
1 parent fe293a8 commit 0c452a8

File tree

13 files changed

+2851
-0
lines changed

13 files changed

+2851
-0
lines changed

examples/with-sequelize/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Sequelize example
2+
3+
This is an example of how to use GraphQL Authentication with [Sequelize](http://docs.sequelizejs.com/). There is no adapter package for Sequelize yet, so in this example we write our own adapter (see `SequelizeAdapter.js`).
4+
5+
Sequelize is configured with SQLite, but only because it doesn't require you to manually start a database so this example stays simple. It also works with MySQL, Postgres etc.
6+
7+
## Usage
8+
9+
```
10+
npm i
11+
npm start
12+
```
13+
14+
Go to http://localhost:4000 in your browser and start executing some queries and mutations!
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const { User } = require('./database');
2+
3+
// There currently is no package for a sequelize adapter, so we create one ourselves!
4+
5+
class GraphqlAuthenticationSequelizeAdapter {
6+
findUserById(ctx, id, info) {
7+
return User.findById(id);
8+
}
9+
findUserByEmail(ctx, email, info) {
10+
return User.findOne({ where: { email } });
11+
}
12+
async userExistsByEmail(ctx, email) {
13+
const user = await User.count({ where: { email } });
14+
return user > 0;
15+
}
16+
17+
// the _createUser and _updateUser methods are just helper methods, they are not used by graphql-authentication.
18+
_createUser(ctx, data) {
19+
return User.create(data);
20+
}
21+
_updateUser(ctx, userId, data) {
22+
return User.update(data, { where: { id: userId } });
23+
}
24+
25+
createUserBySignup(ctx, data) {
26+
return this._createUser(ctx, data);
27+
}
28+
createUserByInvite(ctx, data) {
29+
return this._createUser(ctx, data);
30+
}
31+
updateUserConfirmToken(ctx, userId, data) {
32+
return this._updateUser(ctx, userId, data);
33+
}
34+
updateUserLastLogin(ctx, userId, data) {
35+
return this._updateUser(ctx, userId, data);
36+
}
37+
updateUserPassword(ctx, userId, data) {
38+
return this._updateUser(ctx, userId, data);
39+
}
40+
updateUserResetToken(ctx, userId, data) {
41+
return this._updateUser(ctx, userId, data);
42+
}
43+
updateUserInfo(ctx, userId, data) {
44+
return this._updateUser(ctx, userId, data);
45+
}
46+
updateUserCompleteInvite(ctx, userId, data) {
47+
return this._updateUser(ctx, userId, data);
48+
}
49+
}
50+
51+
module.exports = { GraphqlAuthenticationSequelizeAdapter };

examples/with-sequelize/database.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const Sequelize = require('sequelize');
2+
const sequelize = new Sequelize({ dialect: 'sqlite' });
3+
4+
const User = sequelize.define('user', {
5+
email: Sequelize.STRING,
6+
password: Sequelize.STRING,
7+
name: Sequelize.STRING,
8+
inviteToken: Sequelize.STRING,
9+
inviteAccepted: Sequelize.BOOLEAN,
10+
emailConfirmed: Sequelize.BOOLEAN,
11+
emailConfirmToken: Sequelize.STRING,
12+
resetToken: Sequelize.STRING,
13+
resetExpires: Sequelize.DATE,
14+
deletedAt: Sequelize.DATE,
15+
lastLogin: Sequelize.DATE,
16+
joinedAt: Sequelize.DATE,
17+
isSuper: Sequelize.BOOLEAN
18+
});
19+
20+
sequelize.sync();
21+
22+
module.exports = { User };
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
p Hi,
2+
p You are invited to join *your project*!
3+
p
4+
a(href=mailAppUrl + '/register/' + email + '/' + inviteToken) Accept invite.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
= `Invited for *your project*!`
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
p Hi,
2+
p You requested a password reset on *your project*.
3+
p
4+
a(href=mailAppUrl + '/login/reset-password/' + email + '/' + resetToken) Reset my password.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
= `Confirm your email on *your project*`
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
p Hi,
2+
p You have just created an account on *your project*! As a last step, please confirm your email:
3+
p
4+
a(href=mailAppUrl + '/confirm-email/' + email + '/' + emailConfirmToken) Confirm email.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
= `Confirm your email on *your project*`

examples/with-sequelize/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "graphql-authentication-with-sequelize-example",
3+
"private": true,
4+
"version": "1.0.0",
5+
"license": "ISC",
6+
"dependencies": {
7+
"email-templates": "^4.0.1",
8+
"graphql-authentication": "^0.5.3",
9+
"graphql-yoga": "^1.14.12",
10+
"sequelize": "^4.38.0",
11+
"sqlite3": "^4.0.2"
12+
},
13+
"scripts": {
14+
"start": "node server.js"
15+
}
16+
}

0 commit comments

Comments
 (0)