This repository has been archived by the owner on Feb 28, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a seed script to insert a test user. #1
- Loading branch information
1 parent
60f1ae7
commit f4723f7
Showing
12 changed files
with
124 additions
and
31 deletions.
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
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,12 @@ | ||
import noop from 'lodash/noop' | ||
|
||
export default function authorize(getInitialProps = noop) { | ||
return ({ res, router, session, isServer, ...ctx }) => { | ||
if (!session.user) { | ||
return isServer | ||
? res.redirect('/login') | ||
: router.replace('/login') | ||
} | ||
return getInitialProps({ res, router, session, isServer, ...ctx }) | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { default as authorize } from './authorize' | ||
export { default as isServer } from './is-server' |
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
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,19 @@ | ||
import path from 'path' | ||
import { createKnex, createRedis } from '../connections' | ||
|
||
(async () => { | ||
const knex = createKnex() | ||
const redis = createRedis() | ||
try { | ||
await redis.flushallAsync() | ||
await knex.seed.run({ | ||
directory: path.join(__dirname, 'dev/') | ||
}) | ||
await knex.destroy() | ||
await redis.quitAsync() | ||
} | ||
catch (err) { | ||
console.error(err) | ||
process.exit(1) | ||
} | ||
})() |
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,16 @@ | ||
import bcrypt from 'bcrypt' | ||
|
||
export const user = { | ||
userId: process.env.ADMIN_USER_ID, | ||
username: process.env.ADMIN_USERNAME, | ||
password: process.env.ADMIN_PASSWORD | ||
} | ||
|
||
export async function seed(knex) { | ||
const saltRounds = process.env.NODE_ENV === 'production' ? 12 : 1 | ||
const password = await bcrypt.hash(user.password, saltRounds) | ||
await knex.raw('truncate table users cascade') | ||
await knex | ||
.insert({ ...user, password }) | ||
.into('users') | ||
} |
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,26 @@ | ||
import chalk from 'chalk' | ||
import * as admin from '../admin' | ||
|
||
export async function seed(knex) { | ||
|
||
const trySeed = (knex => async (script, description) => { | ||
/* eslint-disable no-console */ | ||
console.log(chalk.white(`Running seed script for ${description}...`)) | ||
try { | ||
await script.seed(knex) | ||
console.log(chalk.green('done')) | ||
} | ||
catch (err) { | ||
console.error(err) | ||
console.error(chalk.red(`ERROR:Failed to run seed script for ${description}.`)) | ||
process.exit(1) | ||
} | ||
})(knex) | ||
|
||
console.log('\n') | ||
|
||
await trySeed(admin, 'admin user') | ||
await admin.seed(knex) | ||
|
||
console.log('\n') | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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