Skip to content

Commit

Permalink
feat(auth): update teams with authorization middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
bahdcoder committed Jul 31, 2021
1 parent 163c367 commit b197066
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 102 deletions.
Binary file removed examples/blog/example_bloggg
Binary file not shown.
87 changes: 36 additions & 51 deletions examples/blog/index.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,75 @@
require('dotenv').config()
const { cms } = require('@tensei/cms')
const { auth } = require('@tensei/auth')
const { rest } = require('@tensei/rest')
const { graphql } = require('@tensei/graphql')
const { mde, markdown } = require('@tensei/mde')
const { auth, permission } = require('@tensei/auth')

const seed = require('./seed')

const {
tensei,
welcome,
cors,
resource,
text,
textarea,
dateTime,
slug,
array,
hasMany,
belongsTo,
boolean
belongsToMany
} = require('@tensei/core')

module.exports = tensei()
.root(__dirname)
.resources([
resource('Post')
.fields([
text('Title').rules('required'),
slug('Slug')
.creationRules('required', 'unique:slug')
.unique()
.from('Title'),
markdown('Description').creationRules('required', 'max:255'),
textarea('Content').nullable().rules('required'),
dateTime('Published At').creationRules('required'),
belongsTo('Category').alwaysLoad(),
array('Procedure')
.of('string')
.rules('min:3', 'max:10')
.creationRules('required'),
array('Prices')
.nullable()
.of('decimal')
.rules('max:10', 'min:2')
.creationRules('required')
text('Title').notNullable().rules('required'),
textarea('Description').nullable(),
belongsTo('Category').nullable(),
belongsToMany('Tag'),
belongsToMany('Peg')
])
.icon('library')
.displayField('Title'),
resource('Category')
.fields([
text('Name').notNullable().rules('required'),
textarea('Description'),
belongsTo('User').nullable(),
hasMany('Post')
hasMany('Post'),
belongsToMany('Peg')
])
.displayField('Name')
.displayField('Name'),
resource('Tag').fields([
text('Name').rules('required'),
belongsToMany('Post')
]),
resource('Peg').fields([
text('Name').rules('required'),
belongsToMany('Category'),
belongsToMany('Post'),
belongsTo('Team')
])
])
.plugins([
welcome(),
cms().plugin(),
auth()
.teams()
.verifyEmails()
.twoFactorAuth()
.social('google', {
key: process.env.GOOGLE_KEY,
secret: process.env.GOOGLE_SECRET,
clientCallback: 'http://localhost:1234'
})
.setup(({ user }) => {
user.fields([
hasMany('Category'),
boolean('Accepted Terms And Conditions')
.creationRules('required')
.default(false)
])
})
.configureTokens({
accessTokenExpiresIn: 60 * 60 * 60 * 60 * 60
accessTokenExpiresIn: 60 * 60 * 60 * 60
})
.teamPermissions([
permission('Manage databases')
.description('Manage databases')
.default()
.slug('manage:databases')
])
.setup(({ team }) => {
team.fields([hasMany('Peg')])
})
.refreshTokens()
.plugin(),
rest().plugin(),
graphql().plugin(),
cors(),
mde().plugin()
graphql().plugin()
])
.boot(async ctx => {
await seed(ctx)
})
.databaseConfig({
type: 'sqlite',
dbName: 'db.sqlite'
Expand Down
66 changes: 66 additions & 0 deletions examples/blog/seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const faker = require('faker')

const make = (fn, n = 50) => Array.from({ length: n }, () => fn())

module.exports = async ctx => {
const postsSeeder = () =>
ctx.repositories.posts.create({
title: faker.lorem.sentence(),
description: faker.lorem.paragraph()
})

const tagsSeeder = () =>
ctx.repositories.tags.create({
name: faker.lorem.words(3)
})

const pegsSeeder = () =>
ctx.repositories.pegs.create({
name: faker.lorem.words(3)
})

const categoriesSeeder = () =>
ctx.repositories.categories.create({
name: faker.lorem.words(2),
description: faker.lorem.paragraph()
})

const categories = make(categoriesSeeder, 2)

await ctx.orm.em.persistAndFlush()

for (let index = 0; index < categories.length; index++) {
const category = categories[index]

const posts = make(postsSeeder, 5)

const pegs = make(pegsSeeder, 4)

for (let index = 0; index < pegs.length; index++) {
const peg = pegs[index]

const postsForPegs = make(postsSeeder, 3)

await ctx.orm.em.persistAndFlush(postsForPegs)

peg['posts'] = postsForPegs
}

for (let index = 0; index < posts.length; index++) {
const post = posts[index]

const tags = make(tagsSeeder, 5)

await ctx.orm.em.persistAndFlush(tags)

post.tags = tags
}

await ctx.orm.em.persistAndFlush([...posts, ...pegs])

category.posts = posts
category.pegs = pegs
}

await ctx.orm.em.persistAndFlush(categories)
}
22 changes: 11 additions & 11 deletions packages/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ export class Auth implements AuthContract {
setupFn: AuthSetupFn
} = {
prefix: '',
teamPermissions: [
permission('manage:teams').description('Has all permissions on this team')
],
teamPermissions: [],
autoFillUser: true,
autoFilterForUser: true,
tokenResource: 'Token',
Expand Down Expand Up @@ -315,7 +313,7 @@ export class Auth implements AuthContract {
hasOne(this.config.teamResource, 'currentTeam')
.label(`Current ${this.config.teamResource}`)
.nullable(),
hasMany(this.config.teamResource, 'ownTeams').owner()
hasMany(this.config.teamResource, 'ownTeams')
]
}

Expand Down Expand Up @@ -609,6 +607,7 @@ export class Auth implements AuthContract {
.register(
({
gql,
resources,
currentCtx,
extendRoutes,
databaseConfig,
Expand Down Expand Up @@ -658,12 +657,16 @@ export class Auth implements AuthContract {

extendGraphQlTypeDefs([this.extendGraphQLTypeDefs(gql)])

extendGraphQlQueries(this.extendGraphQlQueries())
extendGraphQlQueries(
this.extendGraphQlQueries(currentCtx().resources)
)
extendRoutes(this.extendRoutes())

if (this.config.teams) {
extendGraphQlTypeDefs([this.teamsInstance.types(gql)])
extendGraphQlQueries(this.teamsInstance.queries())
extendGraphQlQueries(
this.teamsInstance.queries(currentCtx().resources)
)
}

if (this.config.autoFillUser) {
Expand Down Expand Up @@ -986,6 +989,7 @@ export class Auth implements AuthContract {
: []),
route(`Register ${name}`)
.path(this.__getApiPath('register'))
.description(`Register a new ${name}`)
.group('Auth')
.post()
.parameters([
Expand Down Expand Up @@ -1260,14 +1264,10 @@ export class Auth implements AuthContract {
return this
}

private extendGraphQlQueries() {
private extendGraphQlQueries(resources: ResourceContract[]) {
const name = this.__resources.user.data.camelCaseName
const pascalName = this.__resources.user.data.pascalCaseName

const resources: ResourceContract[] = Object.keys(this.__resources).map(
key => (this.__resources as any)[key]
)

return [
graphQlQuery(`Login ${name}`)
.path('login')
Expand Down
Loading

0 comments on commit b197066

Please sign in to comment.