Skip to content

Commit b6ac4f3

Browse files
jimmyleejimmylee
authored andcommitted
Initial commit
0 parents  commit b6ac4f3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+7681
-0
lines changed

.babelrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"presets": [
3+
"stage-1",
4+
"stage-2",
5+
"stage-3",
6+
"react",
7+
"./babel-preset"
8+
]
9+
}

.gitignore

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
.next
2+
.vscode
3+
.package-lock.json
4+
node_modules
5+
# Logs
6+
logs
7+
*.log
8+
npm-debug.log*
9+
10+
# Runtime data
11+
pids
12+
*.pid
13+
*.seed
14+
15+
# Directory for instrumented libs generated by jscoverage/JSCover
16+
lib-cov
17+
18+
# Coverage directory used by tools like istanbul
19+
coverage
20+
21+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
22+
.grunt
23+
24+
# node-waf configuration
25+
.lock-wscript
26+
27+
# Compiled binary addons (http://nodejs.org/api/addons.html)
28+
build/Release
29+
30+
# Dependency directory
31+
node_modules
32+
33+
# Optional npm cache directory
34+
.npm
35+
36+
# Optional REPL history
37+
.node_repl_history
38+
39+
# Env variables
40+
.env
41+
42+
# Finder stuff
43+
.DS_Store

.sequelizerc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
"config": path.resolve('./', 'config.js'),
5+
"models-path": path.resolve('./api/models'),
6+
"seeders-path": path.resolve('./api/seeders'),
7+
"migrations-path": path.resolve('./api/migrations')
8+
};

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Jim
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# next-postgres
2+
3+
This is a minimal example of a forum web application with posts and comments.
4+
5+
Uses:
6+
7+
- [NextJS + Custom Express](https://github.com/zeit/next.js/)
8+
- [Postgres](https://www.postgresql.org/)
9+
- [Styled-JSX](https://github.com/zeit/styled-jsx)
10+
- [Passport with local authentication](http://passportjs.org/)
11+
- [Heroku](https://www.heroku.com)
12+
- [Redux](http://redux.js.org/) + [Higher-Order Components](https://facebook.github.io/react/docs/higher-order-components.html)
13+
- [Babel + Stage-1 + Stage-2 + Stage-3](https://babeljs.io/)
14+
15+
![screen shot 2017-07-19 at 11 52 41 pm](https://user-images.githubusercontent.com/310223/28404296-852923d2-6cdd-11e7-88fb-d51697776866.png)
16+
17+
Getting setup should only take 5 minutes!
18+
19+
Useful for:
20+
21+
- Building 100% JavaScript applications with example code.
22+
- Testing out concepts before bringing them over to real work.
23+
- Getting [server side rendering](https://zeit.co/blog/next2) right.
24+
- Looking like you have cheat codes at a Hackathon.
25+
26+
You can [view the demo on heroku](https://guarded-coast-67601.herokuapp.com/). Below are steps to deploy your own.
27+
28+
Coming soon: a [React Native](https://facebook.github.io/react-native/) application built with [Expo](https://expo.io) that communicates to an API served from here.
29+
30+
31+
32+
## Prerequisites
33+
34+
- I use [Homebrew](https://brew.sh/). Using it makes it easier to follow these steps.
35+
- Install Postgres: `brew install postgres`.
36+
- Install [Node 6+](https://nodejs.org/en/): `brew install node`.
37+
38+
39+
40+
## Quick newbies guide to Postgres
41+
42+
- On OSX, to run Postgres:
43+
44+
```sh
45+
pg_ctl -D /usr/local/var/postgres start
46+
```
47+
48+
- There is a file named `./config.js`.
49+
- That file points to a local database named `testdb`. With the username and password as `test`.
50+
51+
### First time Postgres instructions.
52+
53+
```sh
54+
# Enter Postgres console
55+
psql postgres
56+
57+
# Create a new user for yourself
58+
CREATE ROLE yourname WITH LOGIN PASSWORD 'yourname';
59+
60+
# Allow yourself to create databases
61+
ALTER ROLE yourname CREATEDB;
62+
63+
# Exit Postgres console
64+
\q
65+
66+
# Log in as your new user.
67+
psql postgres -U yourname
68+
69+
# Create a database named: testdb.
70+
# If you change this, update config.js
71+
CREATE DATABASE testdb;
72+
73+
# Give your self privileges
74+
GRANT ALL PRIVILEGES ON DATABASE testdb TO yourname;
75+
76+
# List all of your databases
77+
\list
78+
79+
# Connect to your newly created DB as a test
80+
\connect testdb
81+
82+
# Exit Postgres console
83+
\q
84+
```
85+
86+
You wont return here unless you drop tables. You can also use a GUI if you like.
87+
88+
89+
90+
## Running the app locally
91+
92+
While in the root directory of the repo:
93+
94+
```sh
95+
npm install
96+
npm install -g sequelize-cli
97+
sequelize db:migrate
98+
npm run dev
99+
```
100+
101+
You can now visit `localhost:8000` in the browser of your choice.
102+
103+
104+
105+
## Setup Heroku
106+
107+
Install Heroku.
108+
109+
```sh
110+
npm install -g heroku-cli
111+
heroku login
112+
heroku create
113+
```
114+
115+
Heroku will give you a unique address, like ours: `guarded-coast-67601.herokuapp.com`.
116+
117+
Already have a heroku app to deploy to?
118+
119+
```
120+
heroku git:remote -a name-of-your-heroku-app
121+
```
122+
123+
124+
125+
## Setup Postgres and config vars
126+
127+
Go to https://data.heroku.com, add a datastore, pick Postgres.
128+
129+
You will receive `database`, `host`, `password`, `port`, and `username` values. Here is how you set them:
130+
131+
```sh
132+
# Set variables
133+
heroku config:set PRODUCTION_DATABASE=NAME_PROVIDED_FROM_HEROKU
134+
heroku config:set PRODUCTION_HOST=HOST_PROVIDED_FROM_HEROKU
135+
heroku config:set PRODUCTION_PASSWORD=PASSWORD_PROVIDED_FROM_HEROKU
136+
heroku config:set PRODUCTION_PORT=PORT_PROVIDED_FROM_HEROKU
137+
heroku config:set PRODUCTION_USERNAME=USERNAME_PROVIDED_FROM_HEROKU
138+
139+
# See all of your variables
140+
heroku config
141+
```
142+
143+
Set a secret for [cookie-session](https://github.com/expressjs/cookie-session):
144+
145+
```sh
146+
heroku config:set PRODUCTION_SECRET=PICK_A_SECRET
147+
```
148+
149+
150+
151+
## Deploy to Heroku
152+
153+
```sh
154+
git push heroku master
155+
```
156+
157+
append `--force` if necessary.
158+
159+
160+
161+
## Questions?
162+
163+
Feel free to slang any feels to [@meanjim](https://twitter.com/meanjim).

api/auth.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import expressSession from 'cookie-session';
2+
import { session } from '../config.js';
3+
import bcrypt from 'bcrypt';
4+
import { Strategy } from 'passport-local';
5+
import { User } from './models';
6+
7+
module.exports = (app, passport) => {
8+
const newExpressSession = expressSession({
9+
secret: session.secret,
10+
resave: true,
11+
saveUninitialized: true,
12+
cookie: {
13+
secure: false,
14+
httpOnly: false,
15+
maxAge: 600000,
16+
},
17+
});
18+
19+
app.use(newExpressSession);
20+
app.use(passport.initialize());
21+
app.use(passport.session());
22+
23+
const newLocalStrategyOptions = {
24+
usernameField: 'username',
25+
passwordField: 'password',
26+
session: true,
27+
};
28+
const newLocalStrategy = new Strategy(
29+
newLocalStrategyOptions,
30+
async (username, password, done) => {
31+
try {
32+
const user = await User.findOne({
33+
where: {
34+
username: username,
35+
},
36+
});
37+
38+
if (!user) {
39+
return done(null, false, {
40+
message: 'Incorrect credentials.',
41+
});
42+
}
43+
44+
const hashed = bcrypt.hashSync(password, user.salt);
45+
if (user.password === hashed) {
46+
return done(null, user);
47+
}
48+
49+
return done(null, false, {
50+
message: 'Incorrect credentials.',
51+
});
52+
} catch (err) {
53+
done(null, false, {
54+
message: 'Failed',
55+
});
56+
}
57+
}
58+
);
59+
60+
passport.use(newLocalStrategy);
61+
62+
passport.serializeUser((user, done) => {
63+
return done(null, user.id);
64+
});
65+
66+
passport.deserializeUser(async (id, done) => {
67+
try {
68+
const user = await User.findOne({
69+
where: {
70+
id: id,
71+
},
72+
});
73+
74+
if (!user) {
75+
return done(new Error('Wrong user id.'));
76+
}
77+
78+
return done(null, user);
79+
} catch (err) {
80+
return done(null, false, { message: 'Failed' });
81+
}
82+
});
83+
};

0 commit comments

Comments
 (0)