Skip to content

Instantly share code, notes, and snippets.

@devmnj
Created July 10, 2021 13:26
Show Gist options
  • Save devmnj/b0f9fab8b85d6659988ce2b0e6040ee0 to your computer and use it in GitHub Desktop.
Save devmnj/b0f9fab8b85d6659988ce2b0e6040ee0 to your computer and use it in GitHub Desktop.
Keystone CMS authentication in Nuxtjs
const { Keystone } = require('@keystonejs/keystone');
const { Password, Text, Relationship, Checkbox } = require('@keystonejs/fields');
const { GraphQLApp } = require('@keystonejs/app-graphql');
const { AdminUIApp } = require('@keystonejs/app-admin-ui');
const { NuxtApp } = require('@keystonejs/app-nuxt');
const { MongooseAdapter: Adapter } = require('@keystonejs/adapter-mongoose');
const PROJECT_NAME = 'key-app';
const adapterConfig = { mongoUri: 'mongodb://localhost/key-app' };
const { PasswordAuthStrategy } = require('@keystonejs/auth-password')
const keystone = new Keystone({
adapter: new Adapter(adapterConfig),
});
keystone.createList('Todo', {
schemaDoc: 'A list of things which need to be done',
fields: {
name: { type: Text, schemaDoc: 'This is the thing you need to do' },
assignedTo: {
type: Relationship,
ref: 'User',
many: false,
isRequired: true
}
},
});
keystone.createList('User', {
schemaDoc: 'A list of users',
fields: {
name: {
type: Text,
isUnique: true,
isRequired: true
},
isAdmin: {
type: Checkbox,
isRequired: true
},
password: {
type: Password,
isRequired: true
}
},
});
const authStrategy = keystone.createAuthStrategy(
{
type: PasswordAuthStrategy,
list: 'User',
config: {
identityField: 'name',
secretField: 'password'
}
}
)
module.exports = {
keystone,
apps: [
new GraphQLApp(),
new AdminUIApp({
name: PROJECT_NAME, authStrategy,
}),
new NuxtApp({
srcDir: 'src',
buildDir: 'dist',
}),
],
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment