Last active
August 25, 2020 19:03
-
-
Save DanielMolina33/5bd84ec0ee9f486d051327654fe294a6 to your computer and use it in GitHub Desktop.
These are the scripts for passport.js course at Platzi, put this files in /scripts/mongo
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
// DEBUG=app:* node scripts/mongo/seedApiKeys.js | |
const chalk = require('chalk'); | |
const crypto = require('crypto'); | |
const debug = require('debug')('app:scripts:api-keys'); | |
const MongoLib = require('../../lib/mongo'); | |
const adminScopes = [ | |
'signin:auth', | |
'signup:auth', | |
'read:movies', | |
'create:movies', | |
'update:movies', | |
'delete:movies', | |
'read:user-movies', | |
'create:user-movies', | |
'delete:user-movies' | |
]; | |
const publicScopes = [ | |
'signin:auth', | |
'signup:auth', | |
'read:movies', | |
'read:user-movies', | |
'create:user-movies', | |
'delete:user-movies' | |
]; | |
const apiKeys = [ | |
{ | |
token: generateRandomToken(), | |
scopes: adminScopes | |
}, | |
{ | |
token: generateRandomToken(), | |
scopes: publicScopes | |
} | |
]; | |
function generateRandomToken() { | |
const buffer = crypto.randomBytes(32); | |
return buffer.toString('hex'); | |
} | |
async function seedApiKeys() { | |
try { | |
const mongoDB = new MongoLib(); | |
const promises = apiKeys.map(async apiKey => { | |
await mongoDB.create('api-keys', apiKey); | |
}); | |
await Promise.all(promises); | |
debug(chalk.green(`${promises.length} api keys have been created succesfully`)); // prettier-ignore | |
return process.exit(0); | |
} catch (error) { | |
debug(chalk.red(error)); | |
process.exit(1); | |
} | |
} | |
seedApiKeys(); |
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
// DEBUG=app:* node scripts/mongo/seedMovies.js | |
const chalk = require('chalk'); | |
const debug = require('debug')('app:scripts:movies'); | |
const MongoLib = require('../../lib/mongo'); | |
const { moviesMock } = require('../../utils/mocks/movies'); | |
async function seedMovies() { | |
try { | |
const mongoDB = new MongoLib(); | |
const promises = moviesMock.map(async movie => { | |
await mongoDB.create('movies', movie); | |
}); | |
await Promise.all(promises); | |
debug(chalk.green(`${promises.length} movies have been created succesfully`)); // prettier-ignore | |
return process.exit(0); | |
} catch (error) { | |
debug(chalk.red(error)); | |
process.exit(1); | |
} | |
} | |
seedMovies(); |
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
// DEBUG=app:* node scripts/mongo/seedUsers.js | |
const bcrypt = require('bcrypt'); | |
const chalk = require('chalk'); | |
const debug = require('debug')('app:scripts:users'); | |
const MongoLib = require('../../lib/mongo'); | |
const { config } = require('../../config/index'); | |
const users = [ | |
{ | |
email: 'root@undefined.sh', | |
name: 'ROOT', | |
password: config.defaultAdminPassword, | |
isAdmin: true | |
}, | |
{ | |
email: 'jose@undefined.sh', | |
name: 'Jose Maria', | |
password: config.defaultUserPassword | |
}, | |
{ | |
email: 'maria@undefined.sh', | |
name: 'Maria Jose', | |
password: config.defaultUserPassword | |
} | |
]; | |
async function createUser(mongoDB, user) { | |
const { name, email, password, isAdmin } = user; | |
const hashedPassword = await bcrypt.hash(password, 10); | |
const userId = await mongoDB.create('users', { | |
name, | |
email, | |
password: hashedPassword, | |
isAdmin: Boolean(isAdmin) | |
}); | |
return userId; | |
} | |
async function seedUsers() { | |
try { | |
const mongoDB = new MongoLib(); | |
const promises = users.map(async user => { | |
const userId = await createUser(mongoDB, user); | |
debug(chalk.green('User created with id:', userId)); | |
}); | |
await Promise.all(promises); | |
return process.exit(0); | |
} catch (error) { | |
debug(chalk.red(error)); | |
process.exit(1); | |
} | |
} | |
seedUsers(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment