forked from openshiporg/openship
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keystone.js
222 lines (214 loc) · 5.76 KB
/
keystone.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import { config } from "@keystone-6/core";
import * as cookie from "cookie";
import Iron from "@hapi/iron";
import { createAuth } from "@keystone-6/auth";
import { Role } from "./schemas/Role";
import { User } from "./schemas/User";
import { apiKey } from "./schemas/apiKey";
import { Order } from "./schemas/Order";
import { TrackingDetail } from "./schemas/TrackingDetail";
import { LineItem } from "./schemas/LineItem";
import { CartItem } from "./schemas/CartItem";
import { Shop } from "./schemas/Shop";
import { Channel } from "./schemas/Channel";
import { ChannelMetafield } from "./schemas/ChannelMetafield";
import { ShopMetafield } from "./schemas/ShopMetafield";
import { ChannelItem } from "./schemas/ChannelItem";
import { ShopItem } from "./schemas/ShopItem";
import { Match } from "./schemas/Match";
import { Link } from "./schemas/Link";
import { extendGraphqlSchema } from "./mutations";
import { sendPasswordResetEmail } from "./lib/mail";
import "dotenv/config";
const TOKEN_NAME = "keystonejs-session";
const MAX_AGE = 60 * 60 * 8; // 8 hours
const listKey = "User";
export function statelessSessions({
secret,
maxAge = MAX_AGE,
path = "/",
secure = process.env.NODE_ENV === "production",
ironOptions = Iron.defaults,
domain,
sameSite = "lax",
}) {
if (!secret) {
throw new Error("You must specify a session secret to use sessions");
}
if (secret.length < 32) {
throw new Error("The session secret must be at least 32 characters long");
}
return {
async get({ req, createContext }) {
const apiKey = req.headers["x-api-key"];
if (apiKey) {
const sudoContext = createContext({ sudo: true });
try {
const data = await sudoContext.query.apiKey.findOne({
where: {
id: apiKey,
},
query: `id user { id }`,
});
if (!data?.user?.id) return;
return { itemId: data.user.id, listKey };
} catch (err) {
return;
}
}
const cookies = cookie.parse(req.headers.cookie || "");
const bearer = req.headers.authorization?.replace("Bearer ", "");
const token = bearer || cookies[TOKEN_NAME];
if (!token) return;
try {
return await Iron.unseal(token, secret, ironOptions);
} catch (err) {}
},
async end({ res }) {
res.setHeader(
"Set-Cookie",
cookie.serialize(TOKEN_NAME, "", {
maxAge: 0,
expires: new Date(),
httpOnly: true,
secure,
path,
sameSite,
domain,
})
);
},
async start({ res, data }) {
const sealedData = await Iron.seal(data, secret, {
...ironOptions,
ttl: maxAge * 1000,
});
res.setHeader(
"Set-Cookie",
cookie.serialize(TOKEN_NAME, sealedData, {
maxAge,
expires: new Date(Date.now() + maxAge * 1000),
httpOnly: true,
secure,
path,
sameSite,
domain,
})
);
return sealedData;
},
};
}
const sessionConfig = {
maxAge: 60 * 60 * 24 * 30, // How long they stay signed in?
secret:
process.env.SESSION_SECRET || "this secret should only be used in testing",
};
const { withAuth } = createAuth({
listKey: "User",
identityField: "email",
secretField: "password",
initFirstItem: {
fields: ["name", "email", "password"],
itemData: {
/*
This creates a related role with full permissions, so that when the first user signs in
they have complete access to the system (without this, you couldn't do anything)
*/
role: {
create: {
name: "Admin",
// canCreateTodos: true,
// canManageAllTodos: true,
canSeeOtherUsers: true,
canManageUsers: true,
canManageRoles: true,
canSeeOtherOrders: true,
canManageOrders: true,
canSeeOtherShops: true,
canManageShops: true,
canSeeOtherChannels: true,
canManageChannels: true,
canSeeOtherMatches: true,
canManageMatches: true,
canSeeOtherLinks: true,
canManageLinks: true,
},
},
},
},
sessionData: `
name
role {
id
name
canSeeOtherUsers
canManageUsers
canManageRoles
canSeeOtherOrders
canManageOrders
canSeeOtherShops
canManageShops
canSeeOtherChannels
canManageChannels
canSeeOtherMatches
canManageMatches
canSeeOtherLinks
canManageLinks
}
`,
passwordResetLink: {
sendToken: async ({ itemId, identity, token, context }) => {
await sendPasswordResetEmail(token, identity);
},
tokensValidForMins: 60,
},
});
// withAuth applies the signin functionality to the keystone config
export default withAuth(
config({
// db: { provider: 'sqlite', url: 'file:./app.db' },
// server: {
// cors: { origin: false },
// },
db: {
provider: "postgresql" ?? process.env.DATABASE_PROVIDER,
url: process.env.DATABASE_URL,
useMigrations: true,
},
lists: {
// Schema items go in here
User,
apiKey,
Role,
Order,
TrackingDetail,
LineItem,
CartItem,
Channel,
ChannelMetafield,
ChannelItem,
Shop,
ShopMetafield,
ShopItem,
Match,
Link,
},
extendGraphqlSchema,
ui: {
isAccessAllowed: ({ session }) => !!session,
},
session: statelessSessions(sessionConfig),
experimental: {
enableNextJsGraphqlApiEndpoint: true,
generateNextGraphqlAPI: true,
generateNodeAPI: true,
},
graphql: {
playground: true,
apolloConfig: {
introspection: true,
},
},
})
);