diff --git a/env.example b/env.example index 7367126..b30366f 100644 --- a/env.example +++ b/env.example @@ -23,7 +23,4 @@ OAUTH_REDIRECT_URI=https://.ngrok-free.app/callback SESSION_SECRET= -SUBSCRIBER_REFERENCE= -SUBSCRIBER_PASSWORD= - RELAY_HOST=relay.swire.io diff --git a/index.js b/index.js index c38804b..fcd4a4e 100644 --- a/index.js +++ b/index.js @@ -66,10 +66,10 @@ async function getUserInfo(accessToken) { }) } -async function getSubscriberToken() { +async function getSubscriberToken(reference, password) { const tokenRequest = { - reference: process.env.SUBSCRIBER_REFERENCE, - password: process.env.SUBSCRIBER_PASSWORD, + reference: reference, + password: password, application_id: process.env.OAUTH_APPLICATION_ID, } @@ -92,9 +92,6 @@ app.get('/', async (req, res) => { let token if (req.session && req.session.token) { token = req.session.token - } else { - const response = await getSubscriberToken() - token = response.token } res.render('index', { @@ -109,9 +106,6 @@ app.get('/minimal', async (req, res) => { let token if (req.session && req.session.token) { token = session.token - } else { - const response = getSubscriberToken() - token = response.token } res.render('minimal', { @@ -162,6 +156,30 @@ app.get('/callback', async (req, res) => { } }) +app.get('/subscriber', (req, res) => { + res.render('subscriber') +}) + +app.post('/subscriber', async (req, res) => { + console.log('process subscriber') + + const { reference, password } = req.body; + console.log("reference: ", reference, "password: ", password) + + try { + const tokenData = await getSubscriberToken(reference, password) + const userInfo = await getUserInfo(tokenData.token) + + req.session.token = tokenData.token + req.session.user = userInfo + + res.redirect('/') + } catch (error) { + console.error(error) + res.status(500).send('

An error occurred

' + error.message + '

') + } +}) + app.get('/service-worker.js', async (req, res) => { res.set({ 'Content-Type': 'application/javascript; charset=UTF-8', diff --git a/views/index.ejs b/views/index.ejs index 7a9760a..23650a0 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -29,7 +29,10 @@ Minimal + diff --git a/views/minimal.ejs b/views/minimal.ejs index db6a7e9..db6dd6d 100644 --- a/views/minimal.ejs +++ b/views/minimal.ejs @@ -22,7 +22,10 @@ Minimal + diff --git a/views/subscriber.ejs b/views/subscriber.ejs new file mode 100644 index 0000000..ac713a5 --- /dev/null +++ b/views/subscriber.ejs @@ -0,0 +1,50 @@ + + + + + SignalWire CF Client Beta + + + + + + + + + +
+

Subscriber Signin/Signup

+
+
+ + +
+
+ + +
+ +
+
+ + +