Skip to content

Commit

Permalink
Feature/auth (#15)
Browse files Browse the repository at this point in the history
* add basic auth

* refractor whole first draft of auth
  • Loading branch information
Fraasi authored and gruselhaus committed Nov 18, 2019
1 parent 4721dc2 commit 85c359e
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"@keystonejs/adapter-mongoose": "^5.0.0",
"@keystonejs/app-admin-ui": "^5.0.0",
"@keystonejs/app-graphql": "^5.0.0",
"@keystonejs/auth-password": "^5.0.0",
"@keystonejs/fields": "^5.1.0",
"@keystonejs/keystone": "^5.0.0",
"cross-env": "^5.2.0"
},
Expand Down
39 changes: 38 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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,
}),
],
};
43 changes: 43 additions & 0 deletions src/schema.js
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 },
},
};
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,14 @@
serialize-error "^3.0.0"
stack-utils "^1.0.2"

"@keystonejs/auth-password@^5.0.0":
version "5.0.0"
resolved "https://registry.yarnpkg.com/@keystonejs/auth-password/-/auth-password-5.0.0.tgz#ada37104729e9589b9b51615070ff4db2158a875"
integrity sha512-FCYjvEAPH49RNCH7AuAmHUkoK8VCZRJm6Gqza8YjTAijk9+6Af0updhxLRp9VYhnHSrsfihMP+jTso61dvGDuw==
dependencies:
"@keystonejs/fields" "^5.0.0"
"@keystonejs/session" "^5.0.0"

"@keystonejs/build-field-types@^5.0.0", "@keystonejs/build-field-types@^5.0.1", "@keystonejs/build-field-types@^5.1.0":
version "5.1.0"
resolved "https://registry.yarnpkg.com/@keystonejs/build-field-types/-/build-field-types-5.1.0.tgz#7020796fdb16752b2f5bc973b66c8e15554e2ac1"
Expand Down

0 comments on commit 85c359e

Please sign in to comment.