Skip to content
This repository has been archived by the owner on Aug 24, 2019. It is now read-only.

Commit

Permalink
Init new api
Browse files Browse the repository at this point in the history
  • Loading branch information
onerciller committed Mar 19, 2019
2 parents 0f113ff + b5a5fbb commit 7483deb
Show file tree
Hide file tree
Showing 148 changed files with 11,734 additions and 4,556 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true

# Matches multiple files with brace expansion notation
[*.{js,jsx,html,sass}]
charset = utf-8
indent_style = tab
indent_size = 2
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
51 changes: 3 additions & 48 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,48 +1,3 @@
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
*.manifest
*.spec
pip-log.txt
pip-delete-this-directory.txt
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
*.log
.python-version
celerybeat-schedule
.env
.venv
env/
venv/
ENV/
.ropeproject
/site
.mypy_cache/
.idea
.data
.DS_Store
db.json
node_modules
yarn-error.log
package-lock.json
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"semi": true,
"trailingComma": "es5",
"singleQuote": true
}
8 changes: 8 additions & 0 deletions .sequelizerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const path = require('path');

module.exports = {
"config": path.resolve('config', 'index.js'),
"models-path": path.resolve('models'),
"seeders-path": path.resolve('seeders'),
"migrations-path": path.resolve('migrations')
};
26 changes: 0 additions & 26 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1 @@
language: python

python:
- "3.5"

services:
- mysql
- docker

install:
- python -V
- pip install -r requirements.txt
- mysql -e 'CREATE DATABASE IF NOT EXISTS $DB_NAME;'

test:
script:
- python manage.py migrate
- python manage.py test

script:
- docker build -t factlist/factlist-api .

after_success:
- echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
- if [ "$TRAVIS_BRANCH" == "master" ]; then
docker push factlist/factlist-api;
fi
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<<<<<<< HEAD
FROM python:3.5.2

RUN mkdir /api
Expand All @@ -7,3 +8,11 @@ RUN pip install -r requirements.txt
ADD . /api/

EXPOSE 8000
=======
FROM node:alpine
WORKDIR "/app"
COPY ./package.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "docker"]
>>>>>>> develop
8 changes: 8 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM node:alpine
WORKDIR "/app"
COPY ./package.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "docker"]

EXPOSE 4000
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<p align="center">
<h1 align="center">Factlist</h1>
<p align="center"><a href="http://factlist.com" target="_blank"><img width="150" src="https://avatars2.githubusercontent.com/u/33418877?s=200&v=4" /></a></p>
</p>

## About

The Factlist API service is built with Nodejs, Graphql and latest stable versions of other npm packages.

## Getting started

### Development

- Install npm modules:
```
yarn install
```

- Create database migration
```
yarn db:migrate
```

- Create database seed
```
yarn db:seed
```

- Start server watch
```
yarn watch
```


11 changes: 11 additions & 0 deletions auth/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const Login = require('./local/login');
const Register = require('./local/register');
const Twitter = require('./social/twitter');
const ResetPassword = require('./reset-password');

module.exports = app => {
Login(app);
Register(app);
Twitter(app);
ResetPassword(app);
};
13 changes: 13 additions & 0 deletions auth/local/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const token = require('../../helpers/token');
const passport = require('passport');
const requireSignin = passport.authenticate('local', { session: false });
const config = require('../../config');
require('../../services/passport');

module.exports = app => {
app.post('/auth/login', requireSignin, (req, res) => {
res.send({
token: token.generate({ id: req.user.id }, config.auth.tokenLifeTime)
});
});
};
23 changes: 23 additions & 0 deletions auth/local/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const User = require('../../models').users;
const token = require('../../helpers/token');
const config = require('../../config');
module.exports = app => {
app.post('/auth/register', async (req, res) => {
if (!req.body.email || !req.body.password) {
return res
.status(500)
.send({ error: config.locale.auth.must_provide_email });
}
const user = await User.findOne({ where: { email: req.body.email } });

if (user) {
return res
.status(500)
.send({ error: config.locale.auth.already_use_email });
}
const createdUser = await User.create(req.body);
res.send({
token: token.generate({ id: createdUser.id }, config.auth.tokenLifeTime)
});
});
};
39 changes: 39 additions & 0 deletions auth/reset-password/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const User = require('../../models').users;
const config = require('../../config');

module.exports = app => {
app.post('/auth/reset-password', async(req, res) => {
try {
const token = req.body.token;
const password = req.body.password;
const user = await User.findOne({ token });
if (!user) {
return status(500).send({ error: config.locale.auth.token_invalid });
}
await user.update({ password, token: '' });
res.status(500).send({ success: config.locale.success.global });
} catch (error) {
res.status(500).send({ error });
}
});

app.post('/auth/send-reset-password-mail', async (req, res) => {
try {
const email = req.body.email;

const token = crypto.randomBytes(16).toString('hex');

const user = await User.findOne({ where: { email: email } });

if (!user) {
return res.status(500).send({ error: config.locale.auth.failed });
}

await user.update({ token });
//sns
res.send(200, { success: config.locale.success.global });
} catch (error) {
return res.status(500).send({ error });
}
});
};
55 changes: 55 additions & 0 deletions auth/social/twitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const User = require('../../models').users;
const passport = require('passport');
const TwitterStrategy = require('passport-twitter').Strategy;
const config = require('../../config');
const token = require('../../helpers/token');

const platform = 'twitter';
const authRouter = '/auth/twitter';
const callbackRouter = '/auth/twitter/callback';
const failureRedirect = '/login';
const authMiddleware = passport.authenticate(platform, { failureRedirect });
const twitterOptions = { ...config.auth.twitter, passReqToCallback: true };

const userData = profile => {
return {
twitter: profile.id,
username: profile.username,
name: profile.displayName,
email: profile.emails[0].value
};
};

const twitterStrategy = new TwitterStrategy(
twitterOptions,
async (req, token, tokenSecret, profile, done) => {
try {
let user = await User.findOne({ where: { twitter: profile.id } });
if (!user) {
user = await User.create(userData(profile));
}
done(null, user);
} catch (error) {
done(error, false);
}
}
);

passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((user, done) => done(null, user));

const handleSocialAuth = (req, res) => {
if (!req.user) {
return res.send(config.locale.auth.not_authorized);
}
return res.send({
token: token.generate({ id: req.user.id }, config.auth.tokenLifeTime)
});
};

module.exports = app => {
app.get(authRouter, passport.authenticate(platform));
app.get(callbackRouter, authMiddleware, handleSocialAuth);
};

passport.use(twitterStrategy);

0 comments on commit 7483deb

Please sign in to comment.