-
Notifications
You must be signed in to change notification settings - Fork 4
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
13 changed files
with
390 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
DATABASE_USERNAME_DEV="your-database-username" | ||
DATABASE_PASSWORD="your-database-password" | ||
DATABASE_PASSWORD_TEST="your-postgres-password-test" | ||
SECRET_JWT_KEY="your-own-secret-key" | ||
|
||
DATABASE_USERNAME_TEST="your-database-username-for-test" | ||
DATABASE_PASSWORD_TEST="your-postgres-password-test" | ||
|
||
SENDGRID_API_KEY="insert your send grid token" | ||
SECRET_JWT_KEY='write your secret key' | ||
SECRET_JWT_KEY="your-own-secret-key" | ||
SECRET_KEY="your-database-password" | ||
DATABASE_PASSWORD_TEST="password" | ||
DATABASE_USERNAME_TEST="postgres" | ||
|
||
HEROKU_DATABASE_USERNAME="heroku-database-username" | ||
HEROKU_SECRET_KEY="heroku-database-password" | ||
HEROKU_HOST="heroku-host" | ||
HEROKU_DATABASE="heroku-database-name" | ||
SendGridKey="" | ||
|
||
HOST="127.0.0.1:7000" |
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,59 @@ | ||
import model from '../db/models/index'; | ||
|
||
const { Users } = model; | ||
|
||
/** | ||
* profile controller | ||
*/ | ||
class ProfileManager { | ||
/** | ||
* | ||
* @param {Object} req | ||
* @param {Object} res | ||
* @returns {Object} success user view profile | ||
*/ | ||
static async viewProfile(req, res) { | ||
const user = await Users.findOne({ where: { username: req.params.username } }); | ||
if (user) { | ||
user.hash = undefined; | ||
return res.status(200).json({ | ||
profile: user | ||
}); | ||
} | ||
return res.status(404).json({ | ||
message: 'user with that email does not exist' | ||
}); | ||
} | ||
|
||
/** | ||
* | ||
* @param {Object} req | ||
* @param {Object} res | ||
* @returns {Object} success user update profile | ||
*/ | ||
static async updateProfile(req, res) { | ||
try { | ||
const { | ||
email, username, image, bio, isVerified | ||
} = req.body; | ||
const user = await Users.findOne({ where: { username: req.params.username } }); | ||
const updated = await user.update({ | ||
email: email || user.email, | ||
username: username || user.username, | ||
image: image || user.image, | ||
bio: bio || user.bio, | ||
isVerified: isVerified || user.isVerified | ||
}); | ||
updated.hash = undefined; | ||
return res.status(200).json({ | ||
user: updated | ||
}); | ||
} catch (error) { | ||
return res.json({ | ||
error | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export default ProfileManager; |
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
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,43 @@ | ||
import processToken from '../helpers/processToken'; | ||
|
||
/** | ||
* authenticate user | ||
*/ | ||
class isAuth { | ||
/** | ||
* | ||
* @param {object} req | ||
* @param {object} res | ||
* @param {object} next | ||
* @returns {object} user details | ||
*/ | ||
static async hasToken(req, res, next) { | ||
const { token } = req.headers; | ||
if (!token) { | ||
return res.status(401).json({ | ||
message: 'unauthorized' | ||
}); | ||
} | ||
next(); | ||
} | ||
|
||
/** | ||
* | ||
* @param {object} req | ||
* @param {object} res | ||
* @param {object} next | ||
* @returns {object} user details | ||
*/ | ||
static async isOwner(req, res, next) { | ||
const { token } = req.headers; | ||
const { username } = await processToken.verifyToken(token); | ||
if (username !== req.params.username) { | ||
return res.status(403).json({ | ||
message: 'Forbidden access' | ||
}); | ||
} | ||
next(); | ||
} | ||
} | ||
|
||
export default isAuth; |
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,9 +1,10 @@ | ||
import express from 'express'; | ||
|
||
import users from './users'; | ||
import profiles from './profiles'; | ||
|
||
const router = express.Router(); | ||
|
||
router.use('/api', users); | ||
router.use('/api', profiles); | ||
|
||
export default router; |
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,10 @@ | ||
import express from 'express'; | ||
import profileController from '../../controllers/profileController'; | ||
import isAuth from '../../middlewares/isAuth'; | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/profile/:username', isAuth.hasToken, profileController.viewProfile); | ||
router.put('/profiles/:username', isAuth.hasToken, isAuth.isOwner, profileController.updateProfile); | ||
|
||
export default router; |
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
Oops, something went wrong.