-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
570 additions
and
138 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"users": [ | ||
{ | ||
"id": 1, | ||
"username": "foo", | ||
"password": "bar" | ||
} | ||
] | ||
} |
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,60 @@ | ||
import jsonServer from "json-server"; | ||
import passport from "passport"; | ||
import passportLocal from "passport-local"; | ||
import db from "./db"; | ||
import { findByUsername, findById } from "./users"; | ||
|
||
const LocalStrategy = passportLocal.Strategy; | ||
|
||
passport.use( | ||
new LocalStrategy((username, password, done) => { | ||
process.nextTick(() => { | ||
findByUsername(username, (err, user) => { | ||
if (err) { | ||
return done(err); | ||
} | ||
if (!user) { | ||
return done(null, false, { message: `Unknown user ${username}` }); | ||
} | ||
if (user.password !== password) { | ||
return done(null, false, { message: "Invalid password" }); | ||
} | ||
return done(null, user); | ||
}); | ||
}); | ||
}) | ||
); | ||
|
||
passport.serializeUser((user, cb) => { | ||
cb(null, user.id); | ||
}); | ||
|
||
passport.deserializeUser((id, cb) => { | ||
findById(id, (err, user) => { | ||
if (err) { | ||
return cb(err); | ||
} | ||
return cb(null, user); | ||
}); | ||
}); | ||
|
||
const server = jsonServer.create(); | ||
|
||
const router = jsonServer.router(db); | ||
const middlewares = jsonServer.defaults(); | ||
|
||
server.use(middlewares); | ||
server.use(passport.initialize()); | ||
server.use(jsonServer.bodyParser); | ||
|
||
server.post( | ||
"/login", | ||
passport.authenticate("local", { | ||
successRedirect: "/", | ||
}) | ||
); | ||
|
||
server.use(router); | ||
server.listen(3001, () => { | ||
console.log("JSON Server is running"); | ||
}); |
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,27 @@ | ||
import db from "./db"; | ||
|
||
const { users } = db; | ||
|
||
const findById = (id, fn) => { | ||
const idx = id - 1; | ||
if (users[idx]) { | ||
fn(null, users[idx]); | ||
} else { | ||
fn(new Error(`User ${id} does not exist`)); | ||
} | ||
}; | ||
|
||
const findByUsername = (username, cb) => { | ||
process.nextTick(() => { | ||
for (let i = 0, len = users.length; i < len; i += 1) { | ||
const user = users[i]; | ||
if (user.username === username) { | ||
console.log("FOUND"); | ||
return cb(null, user); | ||
} | ||
} | ||
return cb(null, null); | ||
}); | ||
}; | ||
|
||
export { findByUsername, findById }; |
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,4 +1,5 @@ | ||
require('@babel/register')({ | ||
presets: ['@babel/preset-env'], | ||
require("@babel/register")({ | ||
presets: ["@babel/preset-env"], | ||
}); | ||
require('./src/server'); | ||
require("./src/server"); | ||
require("./db/jsonServer"); |
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 { | ||
USER_REGISTER_REQUEST, | ||
USER_REGISTER_SUCCESS, | ||
USER_REGISTER_FAILURE, | ||
register, | ||
} from "./register"; | ||
|
||
import { LOGIN_REQUEST, LOGIN_SUCCESS, LOGIN_FAILURE, login } from "./login"; | ||
|
||
export { | ||
USER_REGISTER_REQUEST, | ||
USER_REGISTER_SUCCESS, | ||
USER_REGISTER_FAILURE, | ||
LOGIN_REQUEST, | ||
LOGIN_SUCCESS, | ||
LOGIN_FAILURE, | ||
register, | ||
login, | ||
}; |
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,36 @@ | ||
import { loginUser } from "../services"; | ||
|
||
export const LOGIN_REQUEST = "LOGIN_REQUEST"; | ||
export const LOGIN_SUCCESS = "LOGIN_SUCCESS"; | ||
export const LOGIN_FAILURE = "LOGIN_FAILURE"; | ||
|
||
const request = () => ({ | ||
type: LOGIN_REQUEST, | ||
}); | ||
|
||
const success = user => ({ | ||
type: LOGIN_SUCCESS, | ||
user, | ||
}); | ||
|
||
const failure = error => ({ | ||
type: LOGIN_FAILURE, | ||
error, | ||
}); | ||
|
||
export const login = (username, password) => { | ||
return dispatch => { | ||
dispatch(request()); | ||
|
||
loginUser(username, password).then( | ||
loggedUser => { | ||
console.log("Loging succesful!"); | ||
dispatch(success(loggedUser)); | ||
}, | ||
error => { | ||
console.log("ERROR: ", error); | ||
dispatch(failure(error)); | ||
} | ||
); | ||
}; | ||
}; |
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,35 @@ | ||
import { registerUser } from "../services"; | ||
|
||
export const USER_REGISTER_REQUEST = "USER_REGISTER_REQUEST"; | ||
export const USER_REGISTER_SUCCESS = "USER_REGISTER_SUCCESS"; | ||
export const USER_REGISTER_FAILURE = "USER_REGISTER_FAILURE"; | ||
|
||
const request = user => ({ | ||
type: USER_REGISTER_REQUEST, | ||
user, | ||
}); | ||
|
||
const success = user => ({ | ||
type: USER_REGISTER_SUCCESS, | ||
user, | ||
}); | ||
|
||
const failure = error => ({ | ||
type: USER_REGISTER_FAILURE, | ||
error, | ||
}); | ||
|
||
export const register = user => { | ||
return dispatch => { | ||
dispatch(request(user)); | ||
|
||
registerUser(user).then( | ||
registeredUser => { | ||
dispatch(success(registeredUser)); | ||
}, | ||
error => { | ||
dispatch(failure(error)); | ||
} | ||
); | ||
}; | ||
}; |
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
Oops, something went wrong.