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

Commit 6e35ef0

Browse files
authored
Create a login page for re-authing a user (#639)
* Create a login page for re-authing a user * include nav
1 parent 4cbfc03 commit 6e35ef0

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

cmd/server/assets/login/_loginscripts.html

+2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
},
2121
headers: { 'X-CSRF-Token': '{{.csrfToken}}' },
2222
contentType: 'application/x-www-form-urlencoded',
23+
{{if not .currentUser}}
2324
success: function(returnData) {
2425
// The user successfully signed in, redirect to realm selection.
2526
window.location.assign('/login/select-realm');
2627
},
28+
{{end}}
2729
error: function(xhr, status, e) {
2830
// There was an error finding the user. Redirect to the
2931
// sign-out page to clear the firebase cookie and any session

cmd/server/assets/login/login.html

+28-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
</head>
1010

1111
<body class="tab-content">
12+
{{if .currentUser}}
13+
{{template "navbar" .}}
14+
{{end}}
1215
<main role="main" class="container">
1316
{{template "flash" .}}
1417

@@ -17,12 +20,16 @@
1720
<div class="col-sm-6">
1821

1922
<div class="card shadow-sm" id="login-div">
23+
{{if .currentUser}}
24+
<div class="card-header">Refresh authentication</div>
25+
{{else}}
2026
<div class="card-header">COVID-19 test verification</div>
27+
{{end}}
2128
<div class="card-body">
2229
<form id="login-form" class="floating-form" action="/" method="POST">
2330
<div class="form-label-group">
2431
<input type="email" id="email" name="email" class="form-control" placeholder="Email address" required
25-
autofocus />
32+
autofocus {{if .currentUser}}disabled value="{{.currentUser.Email}}"{{end}}/>
2633
<label for="email">Email address</label>
2734
</div>
2835

@@ -90,11 +97,15 @@
9097
// Disable the submit button so we only attempt once.
9198
$submit.prop('disabled', true);
9299

100+
{{if .currentUser}}
101+
let credentials = firebase.auth.EmailAuthProvider.credential($email.val(),$password.val());
102+
firebase.auth().currentUser.reauthenticateWithCredential(credentials)
103+
{{else}}
93104
firebase.auth().signInWithEmailAndPassword($email.val(), $password.val())
105+
{{end}}
94106
.then(function(userCredential) {
95-
flash.clear();
96-
})
97-
.catch(function(error) {
107+
loginSuccess();
108+
}).catch(function(error) {
98109
if (error.code == 'auth/multi-factor-auth-required') {
99110
resolver = error.resolver;
100111
populatePinText(resolver.hints);
@@ -140,7 +151,9 @@
140151
let multiFactorAssertion = firebase.auth.PhoneMultiFactorGenerator.assertion(cred);
141152
// Complete sign-in.
142153
resolver.resolveSignIn(multiFactorAssertion)
143-
.catch(function(err) {
154+
.then(function(userCredential) {
155+
loginSuccess();
156+
}).catch(function(err) {
144157
flash.clear();
145158
flash.error(err.message);
146159
$submitPin.prop('disabled', false);
@@ -243,6 +256,16 @@
243256

244257
$factors.append($li);
245258
}
259+
260+
function loginSuccess() {
261+
{{if .loginRedirect}}
262+
window.location.assign('{{.loginRedirect}}');
263+
{{else}}
264+
{{if .currentUser}}
265+
flash.alert('Successfully refreshed auth credentials.');
266+
{{end}}
267+
{{end}}
268+
}
246269
});
247270
</script>
248271
</body>

cmd/server/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ func realMain(ctx context.Context) error {
224224
sub.Use(requireAuth)
225225
sub.Use(rateLimit)
226226
sub.Use(loadCurrentRealm)
227+
sub.Handle("/login", loginController.HandleReauth()).Methods("GET")
227228
sub.Handle("/login/select-realm", loginController.HandleSelectRealm()).Methods("GET", "POST")
228229
sub.Handle("/login/change-password", loginController.HandleResetPassword()).Methods("GET")
229230
sub.Handle("/account", loginController.HandleAccountSettings()).Methods("GET")

pkg/controller/login/reauth.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package login defines the controller for the login page.
16+
package login
17+
18+
import (
19+
"net/http"
20+
)
21+
22+
func (c *Controller) HandleReauth() http.Handler {
23+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
24+
ctx := r.Context()
25+
// No redirect for reauth
26+
c.renderLogin(ctx, w)
27+
})
28+
}

0 commit comments

Comments
 (0)