|
@@ -1,12 +1,11 @@
|
|
|
import express from 'express'
|
|
|
import bodyParser from 'body-parser'
|
|
|
-import morgan from 'morgan'
|
|
|
import mongoose from 'mongoose'
|
|
|
-import passport from 'passport'
|
|
|
-import jwt from 'jwt-simple'
|
|
|
+import bcrypt from 'bcrypt'
|
|
|
+import jwt from 'jsonwebtoken'
|
|
|
+import bhttp from 'bhttp'
|
|
|
|
|
|
import config from './config/database'
|
|
|
-import { configPassport } from './config/passport'
|
|
|
import User from './models/user'
|
|
|
|
|
|
const port = process.env.PORT || 8080
|
|
@@ -14,85 +13,115 @@ const app = express()
|
|
|
|
|
|
app.use(bodyParser.urlencoded({ extended: false }))
|
|
|
app.use(bodyParser.json())
|
|
|
-app.use(morgan('dev'))
|
|
|
-app.use(passport.initialize())
|
|
|
|
|
|
app.get('/', (req, res) => {
|
|
|
res.send(`Express API at http://localhost:${port}/api`)
|
|
|
})
|
|
|
|
|
|
mongoose.connect(config.database)
|
|
|
-configPassport(passport)
|
|
|
const apiRoutes = express.Router()
|
|
|
|
|
|
-apiRoutes.post('/signup', (req, res) => {
|
|
|
- if (!req.body.name || !req.body.password) {
|
|
|
- res.json({ success: false, msg: 'Please pass name and password.' })
|
|
|
- } else {
|
|
|
- const newUser = new User({
|
|
|
- name: req.body.name,
|
|
|
- password: req.body.password
|
|
|
- })
|
|
|
- newUser.save(err => {
|
|
|
- if (err) {
|
|
|
- return res.json({ success: false, msg: 'Username already exists.' })
|
|
|
- }
|
|
|
- res.json({ success: true, msg: 'Successfully created user.' })
|
|
|
- })
|
|
|
+apiRoutes.post('/login', (req, res) => {
|
|
|
+ const { username, password } = req.body
|
|
|
+ if (!username || !password) {
|
|
|
+ res.status(400).json({ success: false, msg: 'Parameters name and password are required' })
|
|
|
+ return
|
|
|
}
|
|
|
-})
|
|
|
|
|
|
-apiRoutes.post('/authenticate', (req, res) => {
|
|
|
- User.findOne({ name: req.body.name }, (err, user) => {
|
|
|
+ User.findOne({ name: username }, (err, user) => {
|
|
|
if (err) {
|
|
|
- throw err
|
|
|
+ res.status(400).json({ success: false, msg: err })
|
|
|
+ return
|
|
|
}
|
|
|
if (!user) {
|
|
|
- res.send({ success: false, msg: 'Authentication failed. User not found.' })
|
|
|
+ res.status(400).json({ success: false, msg: 'Authentication failed. User not found.' })
|
|
|
+ return
|
|
|
+ }
|
|
|
+ console.log(password, user)
|
|
|
+ if (!bcrypt.compareSync(password, user.password)) {
|
|
|
+ res.status(400).json({ success: false, msg: 'Authentication failed. Wrong password' })
|
|
|
} else {
|
|
|
- user.comparePassword(req.body.password, (err, isMatch) => {
|
|
|
- if (isMatch && !err) {
|
|
|
- const token = jwt.encode(user, config.secret)
|
|
|
- res.json({ success: true, token: `JWT ${token}` })
|
|
|
- } else {
|
|
|
- res.send({ soccess: false, msg: 'Authentication failed. Wrong password.' })
|
|
|
- }
|
|
|
- })
|
|
|
+ const token = jwt.sign({
|
|
|
+ exp: Math.floor(Date.now() / 1000) + 24*60*60,
|
|
|
+ data: user
|
|
|
+ }, "bugu")
|
|
|
+ res.json({ success: true, token })
|
|
|
}
|
|
|
})
|
|
|
})
|
|
|
|
|
|
-apiRoutes.get('/memberinfo', passport.authenticate('jwt', { session: false }), (req, res) => {
|
|
|
- const token = getToken(req.headers)
|
|
|
- if (token) {
|
|
|
- const decoded = jwt.decode(token, config.secret)
|
|
|
- User.findOne({ name: decoded.name }, (err, user) => {
|
|
|
- if (err) {
|
|
|
- throw err
|
|
|
- }
|
|
|
- if (!user) {
|
|
|
- return res.status(403).send({ success: false, msg: 'Authentication failed. User not found.' })
|
|
|
- } else {
|
|
|
- res.json({ success: true, msg: `Welcome in the member area, ${user.name}!` })
|
|
|
- }
|
|
|
+const swisstennis = express.Router()
|
|
|
+swisstennis.post('/login', async (req, res) => {
|
|
|
+ const { username, tournament, password } = req.body
|
|
|
+ const session = bhttp.session()
|
|
|
+ if (!username || !password) {
|
|
|
+ res.status(400).json({ success: false, msg: 'Parameters username and password are required' })
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const loginData = {
|
|
|
+ Lang: 'D',
|
|
|
+ id: username,
|
|
|
+ pwd: password,
|
|
|
+ Tournament: ''
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ console.log('attempting to fetch login page.')
|
|
|
+ const reqPage = await session.get('https://comp.swisstennis.ch/advantage/servlet/MyTournamentList?Lang=D')
|
|
|
+ console.log('successfully fetched login page.')
|
|
|
+ } catch (error) {
|
|
|
+ console.log('Error fetching login page.', error)
|
|
|
+ res.status(400).json({
|
|
|
+ msg: error
|
|
|
})
|
|
|
- } else {
|
|
|
- return res.status(403).send({ success: false, msg: 'No token provided.' })
|
|
|
+ return
|
|
|
}
|
|
|
-})
|
|
|
-
|
|
|
-function getToken (headers) {
|
|
|
- if (headers && headers.authorization) {
|
|
|
- const parted = headers.authorization.split(' ')
|
|
|
- if (parted.length === 2) {
|
|
|
- return parted[1]
|
|
|
- } else {
|
|
|
- return null
|
|
|
+ try {
|
|
|
+ console.log('attempting to login.', loginData)
|
|
|
+ const loginPage = await session.post('https://comp.swisstennis.ch/advantage/servlet/Login', loginData)
|
|
|
+ const dec = loginPage.body.toString()
|
|
|
+ console.log('received a page.')
|
|
|
+ if (dec.includes('Zugriff verweigert')) {
|
|
|
+ console.log('failed to log in')
|
|
|
+ res.status(400).json({
|
|
|
+ msg: dec
|
|
|
+ })
|
|
|
+ return
|
|
|
}
|
|
|
- } else {
|
|
|
- return null
|
|
|
+ } catch (error) {
|
|
|
+ console.log('Error logging in.', error)
|
|
|
+ res.status(400).json({
|
|
|
+ msg: error
|
|
|
+ })
|
|
|
+ return
|
|
|
}
|
|
|
-}
|
|
|
+ try {
|
|
|
+ console.log('attempting to fetch my tournaments.')
|
|
|
+ const myTournamentsPage = await session.get('https://comp.swisstennis.ch/advantage/servlet/MyTournamentList?Lang=D')
|
|
|
+ const mdec = myTournamentsPage.body.toString()
|
|
|
+ let match
|
|
|
+ const matches = {}
|
|
|
+ const regexp = /<a href=".*ProtectedDisplayTournament.*tournament=Id(\d+)">([^<]+)<\/a>/gm
|
|
|
+
|
|
|
+ do {
|
|
|
+ match = regexp.exec(mdec)
|
|
|
+ console.log(match)
|
|
|
+ if (match) {
|
|
|
+ matches[match[1]] = match[2]
|
|
|
+ }
|
|
|
+ } while (match)
|
|
|
+ res.json({
|
|
|
+ matches
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ console.log('Error fetching tournaments.')
|
|
|
+ res.status(400).json({
|
|
|
+ msg: error
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+})
|
|
|
+
|
|
|
+apiRoutes.use('/swisstennis', swisstennis)
|
|
|
|
|
|
app.use('/api', apiRoutes)
|
|
|
|