Skip to content

Commit 4a56559

Browse files
committed
add TypeORM example
1 parent fb6aaaf commit 4a56559

File tree

24 files changed

+3253
-1
lines changed

24 files changed

+3253
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ _Previously called Prisma Auth_
55
A very opinionated user authentication package for [GraphQL](https://graphql.org/). It uses old-school email/password authentication.
66

77
This package provides **a GraphQL schema and GraphQL resolvers** for everything you need related to authentication. It does not access your data layer (e.g. an ORM); for that you need to write an _adapter_ (which is not hard to do).
8-
If you use Prisma, there is already an adapter for you, **[graphql-authentication-prisma](https://github.com/Volst/graphql-authentication/tree/master/packages/graphql-authentication-prisma)**.
8+
If you use Prisma, there is already an adapter for you, **[graphql-authentication-prisma](https://github.com/Volst/graphql-authentication/tree/master/packages/graphql-authentication-prisma)**. You can also checkout the [`examples/`](https://github.com/Volst/graphql-authentication/tree/master/examples) folder with examples on how to write an adapter for Sequelize and TypeORM!
99

1010
**Features:**
1111

examples/with-typeorm/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# TypeORM example
2+
3+
This is an example of how to use GraphQL Authentication with [TypeORM](http://typeorm.io/). There is no adapter package for TypeORM yet, so in this example we write our own adapter (see `TypeOrmAdapter.js`).
4+
5+
TypeORM 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 running some queries and mutations!
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { getRepository } from 'typeorm';
2+
import { User } from './database';
3+
import { GraphqlAuthenticationAdapter } from './node_modules/graphql-authentication';
4+
5+
// There currently is no package for a sequelize adapter, so we create one ourselves!
6+
7+
export class GraphqlAuthenticationTypeOrmAdapter
8+
implements GraphqlAuthenticationAdapter {
9+
private db() {
10+
return getRepository(User);
11+
}
12+
async findUserById(ctx: object, id, info) {
13+
return (await this.db().findOne(id)) || null;
14+
}
15+
async findUserByEmail(ctx: object, email, info) {
16+
return (await this.db().findOne({ where: { email } })) || null;
17+
}
18+
async userExistsByEmail(ctx: object, email) {
19+
const user = await this.db().count({ email });
20+
return user > 0;
21+
}
22+
23+
// the _createUser and _updateUser methods are just helper methods, they are not used by graphql-authentication.
24+
async _createUser(ctx: object, data) {
25+
const user = ((await this.db().create(data)) as any) as User;
26+
await this.db().save(user);
27+
return user;
28+
}
29+
async _updateUser(ctx: object, userId, data) {
30+
return await this.db().update(userId, { name: 'kees' })[0];
31+
}
32+
33+
createUserBySignup(ctx: object, data) {
34+
return this._createUser(ctx, data);
35+
}
36+
createUserByInvite(ctx: object, data) {
37+
return this._createUser(ctx, data);
38+
}
39+
updateUserConfirmToken(ctx: object, userId, data) {
40+
return this._updateUser(ctx, userId, data);
41+
}
42+
updateUserLastLogin(ctx: object, userId, data) {
43+
return this._updateUser(ctx, userId, data);
44+
}
45+
updateUserPassword(ctx: object, userId, data) {
46+
return this._updateUser(ctx, userId, data);
47+
}
48+
updateUserResetToken(ctx: object, userId, data) {
49+
return this._updateUser(ctx, userId, data);
50+
}
51+
updateUserInfo(ctx: object, userId, data) {
52+
return this._updateUser(ctx, userId, data);
53+
}
54+
updateUserCompleteInvite(ctx: object, userId, data) {
55+
return this._updateUser(ctx, userId, data);
56+
}
57+
}

examples/with-typeorm/database.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import 'reflect-metadata';
2+
import { createConnection } from 'typeorm';
3+
4+
export { User } from './entities/User';
5+
6+
createConnection();
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*`

0 commit comments

Comments
 (0)