Skip to content

Commit

Permalink
feat(cms): setup index and detail pages for cms
Browse files Browse the repository at this point in the history
- Setup login functionality
- Setup register functionality
- Setup index screen
- Setup csrf protection on dashboard
- Add boolean detail field
- Create resource component for dynamically handling resources
  • Loading branch information
Frantz Kati committed Jan 5, 2021
1 parent a7c1c83 commit 222ba18
Show file tree
Hide file tree
Showing 106 changed files with 6,535 additions and 1,537 deletions.
30 changes: 5 additions & 25 deletions examples/blog/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require('dotenv').config()
const { mde } = require('@tensei/mde')
const { rest } = require('@tensei/rest')
const { auth } = require('@tensei/auth')
const { media } = require('@tensei/media')
Expand Down Expand Up @@ -35,31 +36,6 @@ module.exports = tensei()
),
])
.plugins([
auth()
.user('Customer')
.twoFactorAuth()
.verifyEmails()
.teams()
.apiPath('auth')
.rolesAndPermissions()
.social('github', {
key: process.env.GITHUB_KEY,
secret: process.env.GITHUB_SECRET,
scope: ['user', 'user:email'],
})
.social('gitlab', {
key: process.env.GITLAB_KEY,
secret: process.env.GITLAB_SECRET,
})
.social('google', {
key: process.env.GOOGLE_KEY,
secret: process.env.GOOGLE_SECRET,
})
.social('linkedin', {
key: process.env.LINKEDIN_KEY,
secret: process.env.LINKEDIN_SECRET,
})
.plugin(),
media().graphql().plugin(),
graphql()
.middlewareOptions({
Expand All @@ -81,6 +57,7 @@ module.exports = tensei()
.pass('b0adaac4573cd9')
.port(2525)
.plugin(),
mde().plugin(),
])
.db({
type: process.env.DATABASE_TYPE || 'mysql',
Expand All @@ -89,3 +66,6 @@ module.exports = tensei()
user: process.env.DATABASE_USER || 'mikrotensei',
password: process.env.DATABASE_PASSWORD || '',
})
.boot(({ routes }) => {
// routes.forEach(r => console.log(r.config.path, r.config.id, r.config.authorize.length))
})
8 changes: 6 additions & 2 deletions examples/blog/resources/Comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ const {
} = require('@tensei/core')

module.exports = resource('Comment')
.displayField('Title')
.fields([
text('Title').rules('required').searchable(),
text('Title').rules('required').searchable().sortable(),
textarea('Body').rules('required').hideOnIndex(),
textarea('Reply').rules('required', 'max:255').hideOnIndex(),
textarea('Reply')
.rules('required', 'max:255')
.hideOnIndex()
.alwaysShow(),
belongsTo('Post'),
hasOne('Editor'),
hasMany('Reaction'),
Expand Down
3 changes: 2 additions & 1 deletion examples/blog/resources/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module.exports = resource('Post')
text('Slug').rules('required', 'slug').unique(),
text('Description').rules('required').hideOnIndex(),
// trix('Content').rules('required', 'max:2000', 'min:12').hideOnIndex(),
integer('Av. CPC').rules('required').hideOnDetail(),
integer('Av. CPC').rules('required').hideOnDetail().sortable(),
select('Category')
.options([
{
Expand Down Expand Up @@ -98,6 +98,7 @@ module.exports = resource('Post')
belongsTo('User').searchable().rules('required'),
date('Published At')
.notNullable()
.hideOnIndex()
.firstDayOfWeek(4)
.rules('required', 'date')
.format('do MMM yyyy, hh:mm a'),
Expand Down
2 changes: 1 addition & 1 deletion examples/blog/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ async function seedMongo(resources, connection) {
}

require('./app')
.start()
.start(() => {}, false)
.then(async (tensei) => {
const {
ctx: {
Expand Down
5 changes: 5 additions & 0 deletions packages/auth/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import { CookieOptions } from 'express'
import { AnyEntity } from '@mikro-orm/core'
import { UserRole } from '@tensei/common'
import { ApiContext } from '@tensei/common'

export interface GrantConfig {
key: string
Expand Down Expand Up @@ -45,7 +46,10 @@ export interface AuthPluginConfig {
fields: FieldContract[]
profilePictures: boolean
userResource: string
tokenResource: string
cms: boolean
prefix: string
registered?: (ctx: ApiContext) => Promise<void> | void
csrfEnabled: boolean
disableAutoLoginAfterRegistration: boolean
roleResource: string
Expand Down Expand Up @@ -112,6 +116,7 @@ export const defaultProviderScopes = (
export const USER_EVENTS = {
REGISTERED: 'user::registered',
LOGGED_IN: 'user::logged::in',
ADMIN_REGISTERED: 'admin::registered',
FORGOT_PASSWORD: 'user::forgot::password',
RESET_PASSWORD: 'user::reset::password',
VERIFIED_EMAIL: 'user::verified::email',
Expand Down
10 changes: 7 additions & 3 deletions packages/auth/src/csrf.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Csurf from 'csurf'
import { NextFunction, Request, Response } from 'express'

const Middleware = Csurf({
export const CsurfMiddleware = Csurf({
cookie: true
})

Expand All @@ -13,7 +13,11 @@ export const createCsurfToken = () => (
response: Response,
next: NextFunction
) => {
const _middleware = Middleware(request, response, _nextWithoutCheck(next))
const _middleware = CsurfMiddleware(
request,
response,
_nextWithoutCheck(next)
)

response.cookie('x-csrf-token', request.csrfToken(), {
secure: process.env.NODE_ENV === 'production'
Expand All @@ -22,4 +26,4 @@ export const createCsurfToken = () => (
return _middleware
}

export const checkCsurfToken = () => Middleware
export const checkCsurfToken = () => CsurfMiddleware
Loading

0 comments on commit 222ba18

Please sign in to comment.