Skip to content

Commit

Permalink
Make server side style subscriber explicit and not automatic (#29)
Browse files Browse the repository at this point in the history
* Move to explicit server side style subscriber use

* Move to explicit server side style subscriber use
  • Loading branch information
ryanwi authored Jan 23, 2024
1 parent 965d050 commit c5babf4
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 14 deletions.
3 changes: 0 additions & 3 deletions env.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,4 @@ OAUTH_REDIRECT_URI=https://<foo>.ngrok-free.app/callback

SESSION_SECRET=

SUBSCRIBER_REFERENCE=
SUBSCRIBER_PASSWORD=

RELAY_HOST=relay.swire.io
36 changes: 27 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -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', {
Expand All @@ -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', {
Expand Down Expand Up @@ -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('<h1>An error occurred</h1><p>' + error.message + '</p>')
}
})

app.get('/service-worker.js', async (req, res) => {
res.set({
'Content-Type': 'application/javascript; charset=UTF-8',
Expand Down
5 changes: 4 additions & 1 deletion views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
<a class="nav-link" href="/minimal">Minimal</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/oauth">OAuth</a>
<a class="nav-link" href="/oauth">Subscriber OAuth</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/subscriber">Subscriber Signin/Signup</a>
</li>
</ul>
</div>
Expand Down
5 changes: 4 additions & 1 deletion views/minimal.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
<a class="nav-link" href="/minimal">Minimal</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/oauth">OAuth</a>
<a class="nav-link" href="/oauth">Subscriber OAuth</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/subscriber">Subscriber Signin/Signup</a>
</li>
</ul>
</div>
Expand Down
50 changes: 50 additions & 0 deletions views/subscriber.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html>

<head>
<title>SignalWire CF Client Beta</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">

<script type="text/javascript" src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pako/2.1.0/pako.min.js"></script>
</head>

<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container">
<a class="navbar-brand" href="#">SignalWire CF Client Beta</a>
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="/">Full</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="/minimal">Minimal</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/oauth">OAuth</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/subscriber">Subscriber Signin/Signup</a>
</li>
</ul>
</div>
</nav>

<div class="container mt-5">
<h2>Subscriber Signin/Signup</h2>
<form action="/subscriber" method="post">
<div class="mb-3">
<label for="reference" class="form-label">Reference:</label>
<input type="text" class="form-control" id="reference" name="reference">
</div>
<div class="mb-3">
<label for="password" class="form-label">Password:</label>
<input type="password" class="form-control" id="password" name="password">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>

</body>
</html>

0 comments on commit c5babf4

Please sign in to comment.