-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add basic auth * refractor whole first draft of auth
- Loading branch information
1 parent
4721dc2
commit 85c359e
Showing
4 changed files
with
91 additions
and
1 deletion.
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
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 |
---|---|---|
|
@@ -2,6 +2,8 @@ const { Keystone } = require("@keystonejs/keystone"); | |
const { GraphQLApp } = require("@keystonejs/app-graphql"); | ||
const { AdminUIApp } = require("@keystonejs/app-admin-ui"); | ||
const { MongooseAdapter: Adapter } = require("@keystonejs/adapter-mongoose"); | ||
const { PasswordAuthStrategy } = require("@keystonejs/auth-password"); | ||
const { User, CommunityContribution } = require("./schema.js"); | ||
|
||
const PROJECT_NAME = "codingtrain-api"; | ||
|
||
|
@@ -14,9 +16,44 @@ const PROJECT_NAME = "codingtrain-api"; | |
const keystone = new Keystone({ | ||
name: PROJECT_NAME, | ||
adapter: new Adapter(), | ||
onConnect: async () => { | ||
// setup default admin user & dummy data for testing, but only if user list is empty | ||
const users = await keystone.lists.User.adapter.findAll(); | ||
if (!users.length) { | ||
await keystone.createItems({ | ||
User: [{ username: "admin", password: "password" }], | ||
CommunityContribution: [ | ||
{ | ||
name: "Marla Singer", | ||
email: "[email protected]", | ||
linkToCode: "https://github.com/CodingTrain/project-trainsite-api", | ||
linkToDemo: "http://localhost:3000/admin/", | ||
}, | ||
], | ||
}); | ||
} | ||
}, | ||
}); | ||
|
||
keystone.createList("User", User); | ||
keystone.createList("CommunityContribution", CommunityContribution); | ||
|
||
const authStrategy = keystone.createAuthStrategy({ | ||
type: PasswordAuthStrategy, | ||
list: "User", | ||
config: { | ||
identityField: "username", | ||
secretField: "password", | ||
}, | ||
}); | ||
|
||
module.exports = { | ||
keystone, | ||
apps: [new GraphQLApp(), new AdminUIApp({ enableDefaultRoute: true })], | ||
apps: [ | ||
new GraphQLApp(), | ||
new AdminUIApp({ | ||
enableDefaultRoute: true, | ||
authStrategy, | ||
}), | ||
], | ||
}; |
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,43 @@ | ||
const { Text, Password } = require("@keystonejs/fields"); | ||
|
||
const access = { | ||
userIsAdmin: ({ authentication: { item: user } }) => | ||
Boolean(user && user.isAdmin), | ||
userIsCurrentAuth: ({ authentication: { item: user } }) => { | ||
if (!user) return false; | ||
return true; | ||
}, | ||
}; | ||
|
||
module.exports.User = { | ||
fields: { | ||
username: { type: Text, isRequired: true }, | ||
password: { | ||
type: Password, | ||
isRequired: true, | ||
access: { | ||
read: access.userIsCurrentAuth, | ||
update: access.userIsCurrentAuth, | ||
}, | ||
}, | ||
}, | ||
access: { | ||
// TODO: setup isAdmin for Users if needed later | ||
delete: access.userIsAdmin, | ||
}, | ||
}; | ||
|
||
module.exports.CommunityContribution = { | ||
access: { | ||
create: access.userIsCurrentAuth, | ||
read: () => true, | ||
update: access.userIsCurrentAuth, | ||
delete: access.userIsCurrentAuth, | ||
}, | ||
fields: { | ||
name: { type: Text, isRequired: true }, | ||
email: { type: Text }, | ||
linkToCode: { type: Text, isRequired: true }, | ||
linkToDemo: { type: Text }, | ||
}, | ||
}; |
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