Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions app/components/SignIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ export class SignIn extends React.Component {
});
}
render() {
const { onChange, onSubmit } = this;
const { username, password } = this.state;
return (
<form onSubmit={onSubmit}>
<input value={username} onChange={onChange} name='username' />
<input value={password} onChange={onChange} name='password' />
<form onSubmit={this.onSubmit}>
<input value={username} onChange={this.onChange} name='username' />
<input value={password} onChange={this.onChange} name='password' />
<button>Sign In</button>
</form>
);
Expand Down
12 changes: 5 additions & 7 deletions app/components/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ export class App extends React.Component{
}
render(){
const { auth } = this.state;
const { signIn, logout } = this;
if(!auth.id){
return <SignIn signIn={ signIn }/>
}
else {
if (!auth.id) {
return <SignIn signIn={this.signIn} />;
} else {
return (
<div>
Welcome { auth.username }
<button onClick={ logout }>Logout</button>
Welcome {auth.username}
<button onClick={this.logout}>Logout</button>
</div>
);
}
Expand Down
8 changes: 4 additions & 4 deletions seed.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { db, User } = require('./server/db/db');

const users = [
{username: 'Mac', password: 'iAmTheBest'},
{username: 'Sarah', password: 'KINDA_LAME'},
{username: 'Jackie', password: 'leftUsForAJob'}
]
{ username: 'Mac', password: 'iAmTheBest' },
{ username: 'Ben', password: 'KINDA_LAME' },
{ username: 'Lauren', password: 'superFellow!@#' },
];

const seed = async () => {
await db.sync({force: true});
Expand Down
2 changes: 2 additions & 0 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ app.use(express.json());
app.use(express.static(path.join(__dirname, '..','public')))
app.get('/', (req, res) => res.sendFile(path.join(__dirname, '..', 'public', 'index.html')));

// This is for logging in to our app.
app.post('/api/auth', async (req, res, next) => {
try {
/**
Expand All @@ -23,6 +24,7 @@ app.post('/api/auth', async (req, res, next) => {
}
});

// Trying to verify our token / login
app.get('/api/auth', async (req, res, next) => {
try {
// Our tokens will be sent with the req.header of "authorization"
Expand Down
20 changes: 15 additions & 5 deletions server/db/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const Sequelize = require('sequelize');
const { STRING } = Sequelize;
const jwt = require('jsonwebtoken');

const tokenSecret = process.env.JWTSECRET;
const tokenSecret = 'OUR_SECRET_PHRASE';

const db = new Sequelize(
process.env.DATABASE_URL || 'postgres://localhost/jwt_example',
{logging: false}
{ logging: false }
);

const User = db.define('user', {
Expand All @@ -17,10 +17,14 @@ const User = db.define('user', {
User.byToken = async (token) => {
try {
// Typically we'll need to decode the token to get the information, but our first example is just a user's ID.
const user = await User.findByPk(token);
// const user = await User.findByPk(JSON.parse(token).id);
const verifiedToken = jwt.verify(token, tokenSecret);
console.log('verified token', verifiedToken);
const user = await User.findByPk(verifiedToken.id);
if (user) {
return user;
}
// If the user is not found in the database we need to throw an error
const error = Error('bad credentials');
error.status = 401;
throw error;
Expand All @@ -39,8 +43,14 @@ User.authenticate = async ({ username, password }) => {
},
});
if (user) {
// for now this is just our user's IDs. Later on this will be a JWT
return jwt.sign({id: user.id, username: user.username}, process.env.JWTSECRET);
// This is where our JWT SIGN should go
// return { id: user.id };
const token = jwt.sign(
{ id: user.id, username: user.username },
tokenSecret
);
console.log('token', token);
return token;
}
const error = Error('bad credentials');
error.status = 401;
Expand Down