@@ -6,15 +6,22 @@ import {
6
6
createAccountsCoreModule ,
7
7
} from '@accounts/module-core' ;
8
8
import { createAccountsPasswordModule } from '@accounts/module-password' ;
9
- import { AccountsPassword } from '@accounts/password' ;
9
+ import {
10
+ AccountsPassword ,
11
+ infosMiddleware ,
12
+ resetPassword ,
13
+ resetPasswordForm ,
14
+ verifyEmail ,
15
+ } from '@accounts/password' ;
10
16
import { AccountsServer , AuthenticationServicesToken , ServerHooks } from '@accounts/server' ;
11
17
import gql from 'graphql-tag' ;
12
18
import mongoose from 'mongoose' ;
13
19
import { createApplication } from 'graphql-modules' ;
14
20
import { createAccountsMongoModule } from '@accounts/module-mongo' ;
15
- import { createServer } from 'node:http' ;
16
21
import { createYoga } from 'graphql-yoga' ;
17
22
import { useGraphQLModules } from '@envelop/graphql-modules' ;
23
+ import express from 'express' ;
24
+ import helmet from 'helmet' ;
18
25
19
26
void ( async ( ) => {
20
27
// Create database connection
@@ -79,10 +86,14 @@ void (async () => {
79
86
} ,
80
87
} ;
81
88
89
+ const port = 4000 ;
90
+ const siteUrl = `http://localhost:${ port } ` ;
82
91
const app = createApplication ( {
83
92
modules : [
84
- createAccountsCoreModule ( { tokenSecret : 'secret' } ) ,
93
+ createAccountsCoreModule ( { tokenSecret : 'secret' , siteUrl } ) ,
85
94
createAccountsPasswordModule ( {
95
+ requireEmailVerification : true ,
96
+ sendVerificationEmailAfterSignup : true ,
86
97
// This option is called when a new user create an account
87
98
// Inside we can apply our logic to validate the user fields
88
99
validateNewUser : ( user ) => {
@@ -127,11 +138,39 @@ void (async () => {
127
138
context : ( ctx ) => context ( ctx , { createOperationController } ) ,
128
139
} ) ;
129
140
130
- // Pass it into a server to hook into request handlers.
131
- const server = createServer ( yoga ) ;
141
+ const yogaRouter = express . Router ( ) ;
142
+ // GraphiQL specefic CSP configuration
143
+ yogaRouter . use (
144
+ helmet ( {
145
+ contentSecurityPolicy : {
146
+ directives : {
147
+ 'style-src' : [ "'self'" , 'unpkg.com' ] ,
148
+ 'script-src' : [ "'self'" , 'unpkg.com' , "'unsafe-inline'" ] ,
149
+ 'img-src' : [ "'self'" , 'raw.githubusercontent.com' ] ,
150
+ } ,
151
+ } ,
152
+ } )
153
+ ) ;
154
+ yogaRouter . use ( yoga ) ;
155
+
156
+ const router = express . Router ( ) ;
157
+ // By adding the GraphQL Yoga router before the global helmet middleware,
158
+ // you can be sure that the global CSP configuration will not be applied to the GraphQL Yoga endpoint
159
+ router . use ( yoga . graphqlEndpoint , yogaRouter ) ;
160
+ // Add the global CSP configuration for the rest of your server.
161
+ router . use ( helmet ( ) ) ;
162
+ router . use ( express . urlencoded ( { extended : true } ) ) ;
163
+
164
+ router . use ( infosMiddleware ) ;
165
+ router . get ( '/verify-email/:token' , verifyEmail ( app . injector ) ) ;
166
+ router . get ( '/reset-password/:token' , resetPasswordForm ) ;
167
+ router . post ( '/resetPassword' , resetPassword ( app . injector ) ) ;
168
+
169
+ const expressApp = express ( ) ;
170
+ expressApp . use ( router ) ;
132
171
133
172
// Start the server and you're done!
134
- server . listen ( 4000 , ( ) => {
135
- console . info ( ' Server is running on http://localhost:4000/ graphql' ) ;
173
+ expressApp . listen ( port , ( ) => {
174
+ console . info ( ` Server is running on ${ siteUrl } / graphql` ) ;
136
175
} ) ;
137
176
} ) ( ) ;
0 commit comments