Skip to content

Commit

Permalink
not working
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsantosbh committed Jan 10, 2024
1 parent a527ce7 commit a7e227a
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 26 deletions.
85 changes: 60 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,52 @@ require('dotenv').config();
const express = require('express');
const app = express();
const axios = require('axios');
const ClientOAuth2 = require('client-oauth2')

app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static('public'));

app.set('trust proxy')


import('@auth/express').then(({ ExpressAuth }) => {
app.use('/api/auth/*', ExpressAuth({
providers: [
{
id: 'signalwire',
name: 'SignalWire',
type: 'oauth',
authorization: {
url: process.env.OAUTH_AUTH_URI,
params: { scope: 'email' }
},
clientId: process.env.OAUTH_CLIENT_ID,
clientSecret: process.env.OAUTH_SECRET,
token: process.env.OAUTH_TOKEN_URI,
userinfo: process.env.OAUTH_REDIRECT_URI,
profile(profile) {
console.log('$$$$$$', profile);
return {
id: profile.id,
email: profile.email,
first_name: profile.first_name,
last_name: profile.last_name,
display_name: profile.display_name,
job_title: profile.job_title,
push_notification_key: profile.push_notification_key
};
}
}
]
}))

app.listen(process.env.PORT || 3000, () => {
console.log("Server running on port 3000");
});
})


const FIREBASE_CONFIG = JSON.stringify({
apiKey: process.env.FIREBASE_API_KEY,
authDomain: process.env.FIREBASE_AUTH_DOMAIN,
Expand All @@ -28,15 +67,16 @@ const token_request = {

const host = process.env.RELAY_HOST

const oauthConfig = {
clientId: process.env.OAUTH_CLIENT_ID,
clientSecret: process.env.OAUTH_SECRET,
accessTokenUri: process.env.OAUTH_TOKEN_URI,
authorizationUri: process.env.OAUTH_AUTH_URI,
redirectUri: process.env.OAUTH_REDIRECT_URI


async function authtentication(req, res, next) {
console.log(JSON.stringify(req.params))
console.log(JSON.stringify(req.cookies))
console.log(JSON.stringify(req.body))
console.log(JSON.stringify(req.headers))
return res.redirect("/api/auth/signin")
}

const oauthClient = new ClientOAuth2(oauthConfig)

async function apiRequest(endpoint, payload = {}, method = 'POST') {
var url = `https://${process.env.SIGNALWIRE_SPACE}${endpoint}`
Expand All @@ -50,7 +90,7 @@ async function apiRequest(endpoint, payload = {}, method = 'POST') {
return resp.data
}

app.get('/', async (req, res) => {
app.get('/', authtentication, async (req, res) => {
const response = await apiRequest('/api/fabric/subscribers/tokens', token_request)
res.render('index', {
host,
Expand All @@ -70,22 +110,20 @@ app.get('/minimal', async (req, res) => {
});
});

app.get('/oauth', (req, res) => {
const authorizationUri = oauthClient.code.getUri()

res.redirect(authorizationUri)
app.get('/oauth', authtentication, (req, res) => {
res.send(200)
});

app.get('/callback', async (req, res) => {
const credentials = await oauthClient.code.getToken(req.originalUrl);
// app.get('/callback', async (req, res) => {
// const credentials = await oauthClient.code.getToken(req.originalUrl);

res.render('index', {
host,
token: credentials.accessToken,
destination: process.env.DEFAULT_DESTINATION,
firebaseConfig: FIREBASE_CONFIG,
});
})
// res.render('index', {
// host,
// token: credentials.accessToken,
// destination: process.env.DEFAULT_DESTINATION,
// firebaseConfig: FIREBASE_CONFIG,
// });
// })

app.get('/service-worker.js', async (req, res) => {
res.set({
Expand All @@ -97,6 +135,3 @@ app.get('/service-worker.js', async (req, res) => {
});
});

app.listen(process.env.PORT || 3000, () => {
console.log("Server running on port 3000");
});
96 changes: 96 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
"dev": "nodemon index.js",
"test": "npm run test:unit",
"test:unit": "jest --passWithNoTests"

},
"author": "",
"license": "ISC",
"dependencies": {
"@auth/express": "^0.1.2",
"axios": "^1.5.0",
"client-oauth2": "^4.3.3",
"dotenv": "^16.3.1",
Expand Down

0 comments on commit a7e227a

Please sign in to comment.