· 4 years ago · Mar 02, 2021, 08:54 PM
1const express = require('express')
2const axios = require('axios')
3const server = require('./config.js')
4const bodyParser = require('body-parser')
5const cors = require('cors')
6const { response } = require('express')
7const app = express()
8app.use(
9 cors({
10 origin: server.nuxt
11 }),
12 bodyParser.json()
13)
14app.get('/', function(req, res) {
15 res.send('Security!')
16})
17app.post('/login', function(req, res) {
18 var body = {
19 username: req.body.username,
20 password: req.body.password,
21 grant_type: 'password',
22 client_id: server.client,
23 client_secret: server.secret,
24 scope: '*'
25 }
26
27 axios.post(server.laravel + '/oauth/token', body, {
28 headers: {
29 'Content-Type': 'application/json',
30 'Accept': 'application/json'
31 }
32 })
33 .then((response) => {
34 res.send(response.data)
35 })
36 .catch((errors) => {
37 res.status(errors.response.status)
38 res.send(errors.response.data)
39 })
40
41})
42app.post('/refresh', function(req, res) {
43 var body = {
44 refresh_token: req.body.refresh_token,
45 grant_type: 'refresh_token',
46 client_id: server.client,
47 client_secret: server.secret,
48 scope: '*'
49 }
50 axios
51 .post(server.laravel + '/oauth/token', body, {
52 headers: {
53 'Content-Type': 'application/json',
54 'Accept': 'application/json'
55 }
56 })
57 .then((response) => {
58 res.send(response.data)
59 })
60 .catch((errors) => {
61 res.status(errors.response.status)
62 res.send(errors.response.data)
63 })
64})
65app.listen(server.port, 'localhost')
66