Skip to content
This repository was archived by the owner on Jul 12, 2023. It is now read-only.

Create a password selection page #568

Merged
merged 2 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions cmd/server/assets/login/select-password.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{{define "login/select-password"}}
<!doctype html>
<html lang="en">

<head>
{{template "floatingform" .}}
{{template "head" .}}
{{template "firebase" .}}
</head>

<body class="bg-light">
<main role="main" class="container">
{{template "flash" .}}

<div class="d-flex vh-100">
<div class="d-flex w-100 justify-content-center align-self-center">
<div class="col-sm-6">
<div class="card shadow-sm">
<div class="card-header">Select new password</div>
<div class="card-body">
<form id="loginForm" class="floating-form" action="/" method="POST">
<div class="form-label-group">
<input type="email" id="email" name="email" class="form-control" placeholder="Email address" required
autofocus />
<label for="email">Email address</label>
</div>

<div class="form-label-group mb-2">
<input type="password" id="password" name="password" class="form-control" placeholder="Password"
autocomplete="new-password" required />
<label for="password">Password</label>
</div>
<div class="form-label-group">
<input type="password" id="retype" name="retype" class="form-control" placeholder="Retype password"
autocomplete="new-password" required />
<label for="retype">Retype password</label>
</div>

<button type="submit" id="submit" class="btn btn-primary btn-block">Set password</button>
</form>
</div>
<div class="card-body">
<a class="card-link" href="/">&larr; Login</a>
</div>
</div>
</div>
</div>
</div>
</main>

{{template "scripts" .}}
{{template "loginscripts" .}}

<script type="text/javascript">
$(function() {
let $form = $('#loginForm');
let $submit = $('#submit');
let $email = $('#email');
let $password = $('#password');
let $retype = $('#retype');

let urlVars = getUrlVars();
let code = urlVars["oobCode"];

firebase.auth().verifyPasswordResetCode(code ?? "")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?? is not supported in IE

.then(function(email) {
$email.val(email);
}).catch(function(error) {
flash.error("Invalid password reset code. "
+ "The code may be malformed, expired, or has already been used.");
$submit.prop('disabled', true);
});

$form.on('submit', function(event) {
event.preventDefault();

let email = $email.val();
let pwd = $password.val();
if (pwd != $retype.val()) {
flash("Password and retyped passwords must match.", "danger");
return;
}

// Disable the submit button so we only attempt once.
$submit.prop('disabled', true);

firebase.auth().confirmPasswordReset(code, pwd)
.then(function() {
flash.alert("New password confirmed.");
}).catch(function(error) {
clearExistingFlash();
flash.error(error)
$submit.prop('disabled', false);
});
});
});

function getUrlVars() {
let vars = [], hash;
let queryParams = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < queryParams.length; i++) {
v = queryParams[i].split('=');
vars.push(v[0]);
vars[v[0]] = v[1];
}
return vars;
}
</script>
</body>

</html>
{{end}}
1 change: 1 addition & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ func realMain(ctx context.Context) error {

sub.Handle("/", loginController.HandleLogin()).Methods("GET")
sub.Handle("/login/reset-password", loginController.HandleResetPassword()).Methods("GET")
sub.Handle("/login/select-password", loginController.HandleSelectPassword()).Methods("GET")
sub.Handle("/session", loginController.HandleCreateSession()).Methods("POST")
sub.Handle("/signout", loginController.HandleSignOut()).Methods("GET")

Expand Down
32 changes: 32 additions & 0 deletions pkg/controller/login/select_password.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package login defines the controller for the login page.
package login

import (
"net/http"

"github.com/google/exposure-notifications-verification-server/pkg/controller"
)

func (c *Controller) HandleSelectPassword() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

m := controller.TemplateMapFromContext(ctx)
m["firebase"] = c.config.Firebase
c.h.RenderHTML(w, "login/select-password", m)
})
}