Browse Source

Index.html multi user-authentication

pull/665/head
flavio 3 years ago
parent
commit
b7d7129989
  1. 1
      src/config.js
  2. 11
      src/lib/Server.js
  3. 9
      src/www/index.html
  4. 4
      src/www/js/api.js
  5. 7
      src/www/js/app.js

1
src/config.js

@ -4,7 +4,6 @@ const { release } = require('./package.json');
module.exports.RELEASE = release; module.exports.RELEASE = release;
module.exports.PORT = process.env.PORT || 51821; module.exports.PORT = process.env.PORT || 51821;
module.exports.PASSWORD = process.env.PASSWORD;
module.exports.WG_PATH = process.env.WG_PATH || '/etc/wireguard/'; module.exports.WG_PATH = process.env.WG_PATH || '/etc/wireguard/';
module.exports.WG_DEVICE = process.env.WG_DEVICE || 'eth0'; module.exports.WG_DEVICE = process.env.WG_DEVICE || 'eth0';
module.exports.WG_HOST = process.env.WG_HOST; module.exports.WG_HOST = process.env.WG_HOST;

11
src/lib/Server.js

@ -39,15 +39,11 @@ module.exports = class Server {
return RELEASE; return RELEASE;
}))) })))
// Authentication // Get session data
.get('/api/session', Util.promisify(async req => { .get('/api/session', Util.promisify(async req => {
const requiresPassword = !!process.env.PASSWORD; const authenticated = !!(req.session && req.session.authenticated);
const authenticated = requiresPassword
? !!(req.session && req.session.authenticated)
: true;
return { return {
requiresPassword,
authenticated, authenticated,
}; };
})) }))
@ -79,9 +75,6 @@ module.exports = class Server {
// WireGuard // WireGuard
.use((req, res, next) => { .use((req, res, next) => {
if (!PASSWORD) {
return next();
}
if (req.session && req.session.authenticated) { if (req.session && req.session.authenticated) {
return next(); return next();

9
src/www/index.html

@ -24,7 +24,7 @@
<div v-cloak class="container mx-auto max-w-3xl"> <div v-cloak class="container mx-auto max-w-3xl">
<div v-if="authenticated === true"> <div v-if="authenticated === true">
<span v-if="requiresPassword" <span
class="text-sm text-gray-400 mb-10 mr-2 mt-3 cursor-pointer hover:underline float-right" @click="logout"> class="text-sm text-gray-400 mb-10 mr-2 mt-3 cursor-pointer hover:underline float-right" @click="logout">
Logout Logout
<svg class="h-3 inline" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" <svg class="h-3 inline" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
@ -475,6 +475,9 @@
</svg> </svg>
</div> </div>
<input type="text" name="username" placeholder="Username" v-model="username"
class="px-3 py-2 text-sm text-gray-500 mb-5 border-2 border-gray-100 rounded-lg w-full focus:border-red-800 outline-none" />
<input type="password" name="password" placeholder="Password" v-model="password" <input type="password" name="password" placeholder="Password" v-model="password"
class="px-3 py-2 text-sm text-gray-500 mb-5 border-2 border-gray-100 rounded-lg w-full focus:border-red-800 outline-none" /> class="px-3 py-2 text-sm text-gray-500 mb-5 border-2 border-gray-100 rounded-lg w-full focus:border-red-800 outline-none" />
@ -488,10 +491,10 @@
</path> </path>
</svg> </svg>
</button> </button>
<input v-if="!authenticating && password" type="submit" <input v-if="!authenticating && username && password" type="submit"
class="bg-red-800 w-full rounded shadow py-2 text-sm text-white hover:bg-red-700 transition cursor-pointer" class="bg-red-800 w-full rounded shadow py-2 text-sm text-white hover:bg-red-700 transition cursor-pointer"
value="Sign In"> value="Sign In">
<input v-if="!authenticating && !password" type="submit" <input v-if="!authenticating && (!username || !password)" type="submit"
class="bg-gray-200 w-full rounded shadow py-2 text-sm text-white cursor-not-allowed" value="Sign In"> class="bg-gray-200 w-full rounded shadow py-2 text-sm text-white cursor-not-allowed" value="Sign In">
</form> </form>
</div> </div>

4
src/www/js/api.js

@ -43,11 +43,11 @@ class API {
}); });
} }
async createSession({ password }) { async createSession({ username, password }) {
return this.call({ return this.call({
method: 'post', method: 'post',
path: '/session', path: '/session',
body: { password }, body: { username, password },
}); });
} }

7
src/www/js/app.js

@ -28,8 +28,8 @@ new Vue({
data: { data: {
authenticated: null, authenticated: null,
authenticating: false, authenticating: false,
username: null,
password: null, password: null,
requiresPassword: null,
clients: null, clients: null,
clientsPersist: {}, clientsPersist: {},
@ -173,17 +173,18 @@ new Vue({
login(e) { login(e) {
e.preventDefault(); e.preventDefault();
if (!this.username) return;
if (!this.password) return; if (!this.password) return;
if (this.authenticating) return; if (this.authenticating) return;
this.authenticating = true; this.authenticating = true;
this.api.createSession({ this.api.createSession({
username: this.username,
password: this.password, password: this.password,
}) })
.then(async () => { .then(async () => {
const session = await this.api.getSession(); const session = await this.api.getSession();
this.authenticated = session.authenticated; this.authenticated = session.authenticated;
this.requiresPassword = session.requiresPassword;
return this.refresh(); return this.refresh();
}) })
.catch(err => { .catch(err => {
@ -191,6 +192,7 @@ new Vue({
}) })
.finally(() => { .finally(() => {
this.authenticating = false; this.authenticating = false;
this.username = null;
this.password = null; this.password = null;
}); });
}, },
@ -251,7 +253,6 @@ new Vue({
this.api.getSession() this.api.getSession()
.then(session => { .then(session => {
this.authenticated = session.authenticated; this.authenticated = session.authenticated;
this.requiresPassword = session.requiresPassword;
this.refresh({ this.refresh({
updateCharts: true, updateCharts: true,
}).catch(err => { }).catch(err => {

Loading…
Cancel
Save