Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

SSR: Port user profile page.

Kailash Nadh18fe4095a30a
diff --git a/cmd/handlers.go b/cmd/handlers.go
index 50462439..fd2ec38b 100644
--- a/cmd/handlers.go
+++ b/cmd/handlers.go
@@ -106,6 +106,7 @@ func initHTTPHandlers(e *echo.Echo, a *App) {
g.GET(path.Join(uriAdmin, "/users/roles/lists/:id"), hasID(a.ViewListRole))
g.GET(path.Join(uriAdmin, "/users/roles/:id"), hasID(a.ViewUserRole))
g.GET(path.Join(uriAdmin, "/users/:id"), hasID(a.ViewUser))
+ g.GET(path.Join(uriAdmin, "/user/profile"), a.ViewProfile)
// Settings.
g.GET(path.Join(uriAdmin, "/settings"), a.ViewSettings)
diff --git a/cmd/users.go b/cmd/users.go
index 07691e46..2ff0d959 100644
--- a/cmd/users.go
+++ b/cmd/users.go
@@ -102,6 +102,28 @@ func (a *App) ViewUser(c echo.Context) error {
return c.Render(http.StatusOK, "admin-user", data)
}
+// profileView is the admin page view for the logged in user's profile.
+type profileView struct {
+ adminView
+
+ User auth.User
+}
+
+// ViewProfile renders the HTML view for the logged in user's profile.
+func (a *App) ViewProfile(c echo.Context) error {
+ user := auth.GetUser(c)
+
+ // Blank out the password hash before sending (obviously!).
+ user.Password = null.String{}
+
+ data := profileView{
+ adminView: newAdminView(c, a.i18n.T("users.profile"), "", "users.profile"),
+ User: user,
+ }
+
+ return c.Render(http.StatusOK, "admin-profile", data)
+}
+
// getRoles fetches user and list roles for the user-form role selectors.
func (a *App) getRoles() ([]auth.Role, []auth.ListRole, error) {
userRoles, err := a.core.GetRoles()
@@ -378,10 +400,19 @@ func (a *App) UpdateUserProfile(c echo.Context) error {
// EnableTOTP enables TOTP 2FA for a user after verifying the code.
func (a *App) EnableTOTP(c echo.Context) error {
+ u := c.Get(auth.UserHTTPCtxKey).(auth.User)
+
+ // Incoming request.
+ var req struct {
+ Secret string `json:"secret"`
+ Code string `json:"code"`
+ }
+ if err := c.Bind(&req); err != nil {
+ return err
+ }
var (
- u = c.Get(auth.UserHTTPCtxKey).(auth.User)
- secret = strings.TrimSpace(c.FormValue("secret"))
- code = strings.TrimSpace(c.FormValue("code"))
+ secret = strings.TrimSpace(req.Secret)
+ code = strings.TrimSpace(req.Code)
)
if secret == "" || code == "" {
@@ -414,10 +445,16 @@ func (a *App) EnableTOTP(c echo.Context) error {
// DisableTOTP disables TOTP 2FA for a user after verifying the password.
func (a *App) DisableTOTP(c echo.Context) error {
- var (
- u = c.Get(auth.UserHTTPCtxKey).(auth.User)
- password = c.FormValue("password")
- )
+ u := c.Get(auth.UserHTTPCtxKey).(auth.User)
+
+ // Incoming request.
+ var req struct {
+ Password string `json:"password"`
+ }
+ if err := c.Bind(&req); err != nil {
+ return err
+ }
+ password := req.Password
// TOTP isn't enabled.
if u.TwofaType != models.TwofaTypeTOTP {
diff --git a/static/admin/src/js/views/profile.js b/static/admin/src/js/views/profile.js
new file mode 100644
index 00000000..1703a7e0
--- /dev/null
+++ b/static/admin/src/js/views/profile.js
@@ -0,0 +1,100 @@
+import {
+ api,
+ i18n,
+} from '../main.js';
+import * as u from '../utils.js';
+
+// profileView is the logged in user's own profile page.
+function profileView() {
+ const user = window._user || {};
+
+ return {
+ form: {
+ name: user.name || '',
+ email: user.email || '',
+ password: '',
+ password2: '',
+ },
+
+ // TOTP enable flow.
+ showTotpSetup: false,
+ totpQR: null,
+ totpSecret: null,
+ totpCode: '',
+
+ // TOTP disable flow.
+ showDisableTotp: false,
+ disablePassword: '',
+
+ async onSubmit() {
+ const payload = { name: this.form.name, email: this.form.email };
+
+ if (this.form.password) {
+ if (this.form.password !== this.form.password2) {
+ u.toast(i18n.t('users.passwordMismatch'), 'danger');
+ return;
+ }
+ payload.password = this.form.password;
+ payload.password2 = this.form.password2;
+ }
+
+ const data = await api('profile.save', '/profile', 'PUT', payload);
+ this.form.password = '';
+ this.form.password2 = '';
+ u.toast(i18n.ts('globals.messages.updated', { name: data.username }));
+ },
+
+ // ===============
+ // TOTP on/off flow.
+ async onEnableTotp() {
+ this.showTotpSetup = true;
+ try {
+ const data = await api('profile.totp', `/users/${user.id}/twofa/totp`, 'GET');
+ this.totpQR = data.qr;
+ this.totpSecret = data.secret;
+ this.$nextTick(() => this.$refs.totpCode && this.$refs.totpCode.focus());
+ } catch (e) {
+ this.resetTotp();
+ }
+ },
+
+ async confirmTotp() {
+ if (!this.totpCode || this.totpCode.length !== 6) {
+ u.toast(i18n.t('globals.messages.invalidValue'), 'danger');
+ return;
+ }
+
+ await api('profile.totp', `/users/${user.id}/twofa`, 'PUT', {
+ secret: this.totpSecret,
+ code: this.totpCode,
+ });
+ u.reload({ message: i18n.t('users.twoFAEnabled') });
+ },
+
+ resetTotp() {
+ this.showTotpSetup = false;
+ this.totpQR = null;
+ this.totpSecret = null;
+ this.totpCode = '';
+ },
+
+ async confirmDisableTotp() {
+ if (!this.disablePassword) {
+ u.toast(i18n.t('globals.messages.invalidFields'), 'danger');
+ return;
+ }
+
+ await api('profile.totp', `/users/${user.id}/twofa`, 'DELETE', { password: this.disablePassword });
+ u.reload({ message: i18n.t('globals.messages.done') });
+ },
+
+ cancelDisableTotp() {
+ this.showDisableTotp = false;
+ this.disablePassword = '';
+ },
+ };
+}
+
+document.addEventListener('alpine:init', () => {
+ window.Alpine.data('profileView', profileView);
+}, { once: true });
diff --git a/static/admin/views/profile.html b/static/admin/views/profile.html
new file mode 100644
index 00000000..611329dc
--- /dev/null
+++ b/static/admin/views/profile.html
@@ -0,0 +1,129 @@
+{{ define "admin-profile" -}}
+{{ template "admin-header" . }}
+{{- $d := .Data -}}
+{{- $u := $d.User -}}
+
+<script>window._user = {{ ToJSON $u }};</script>
+<section class="user-profile" x-data="profileView()">
+ <header class="row page-header">
+ <div class="col-8">
+ <div class="hstack gap-1">
+ <h1 class="page-title">{{ .L.T "users.profile" }}</h1>
+ {{- if $u.UserRole.Name }}<span class="badge">{{ $u.UserRole.Name }}</span>{{ end }}
+ </div>
+ </div>
+ </header>
+
+ <div class="card page-content content">
+ <form method="post" @submit.prevent="onSubmit">
+ <section class="dialog-body">
+ <h4 class="mb-6">@{{ $u.Username }}</h4>
+ {{- if ne $u.Type "api" }}
+ <label data-field>
+ {{ .L.T "subscribers.email" }}
+ <input name="email" type="email" maxlength="200" required {{ if not $u.PasswordLogin }}disabled{{ end }} x-model="form.email" autofocus />
+ </label>
+ {{- end }}
+
+ <label data-field>
+ {{ .L.T "globals.fields.name" }}
+ <input name="name" maxlength="200" x-model="form.name" />
+ </label>
+
+ {{- if $u.PasswordLogin }}
+ <div class="row">
+ <label data-field class="col-6">
+ {{ .L.T "users.password" }}
+ <input type="password" name="password" minlength="8" maxlength="200" autocomplete="new-password" x-model="form.password" />
+ </label>
+ <label data-field class="col-6">
+ {{ .L.T "users.passwordRepeat" }}
+ <input type="password" name="password2" minlength="8" maxlength="200" autocomplete="new-password" x-model="form.password2" />
+ </label>
+ </div>
+ {{- end }}
+ </section>
+
+ <footer class="dialog-foot hstack justify-end mt-6">
+ <button type="submit" data-variant="primary" data-cy="btn-save" :disabled="isLoading('profile.save')" :aria-busy="isLoading('profile.save')">{{ .L.T "globals.buttons.save" }}</button>
+ </footer>
+ </form>
+ </div>
+
+ {{- if $u.PasswordLogin }}
+ <section class="twofa-app-section mt-6">
+ {{- if eq $u.TwofaType "totp" }}
+ <div class="card content">
+ <div class="row">
+ <div class="col-10">
+ <h3 class="hstack gap-1">{{ Icon "check-circle" }} {{ .L.T "users.twoFAEnabled" }}</h3>
+ </div>
+ <div class="col-end align-right">
+ <input type="checkbox" role="switch" aria-label="{{ .L.T "users.twoFA" }}" :checked="!showDisableTotp" @change="$event.target.checked ? cancelDisableTotp() : (showDisableTotp = true)" />
+ </div>
+ </div>
+ <p>{{ .L.Ts "users.twoFAEnabledDesc" "type" "TOTP" }}</p>
+
+ <template x-if="showDisableTotp">
+ <form class="disable-totp mt-5" @submit.prevent="confirmDisableTotp">
+ <label data-field>
+ {{ .L.T "users.password" }}
+ <input type="password" x-model="disablePassword" minlength="8" required />
+ </label>
+ <div class="hstack gap-3">
+ <button type="submit" data-variant="danger">{{ .L.T "globals.buttons.disable" }}</button>
+ <button type="button" class="outline" @click="cancelDisableTotp">{{ .L.T "globals.buttons.cancel" }}</button>
+ </div>
+ </form>
+ </template>
+ </div>
+ {{- else }}
+ <div class="card content">
+ <div class="row mb-4">
+ <div class="col-10">
+ <h3 class="mb-0">{{ .L.T "users.twoFA" }}</h3>
+ </div>
+ <div class="col-end align-right">
+ <input type="checkbox" role="switch" aria-label="{{ .L.T "users.twoFA" }}" :checked="showTotpSetup" @change="$event.target.checked ? onEnableTotp() : cancelTotp()" />
+ </div>
+ </div>
+ <p>{{ .L.T "users.twoFANotEnabled" }}</p>
+
+ <template x-if="totpQR">
+ <div class="qr-app-section totp-setup mt-5">
+ <figure class="mb-6">
+ <img class="card" :src="'data:image/png;base64,' + totpQR" alt="QR Code" />
+ <figcaption class="text-light text-8">{{ .L.T "users.totpScanQR" }}</figcaption>
+ </figure>
+
+ <fieldset>
+ <legend>{{ .L.T "users.totpSecret" }}</legend>
+ <button type="button" class="small" data-variant="secondary" title="{{ .L.T "globals.messages.clickToCopy" }}" aria-label="{{ .L.T "users.totpSecret" }}" @click="copyToClipboard(totpSecret)">
+ <span x-text="totpSecret"></span>
+ </button>
+ </fieldset>
+
+ <form class="mt-5" @submit.prevent="confirmTotp">
+ <fieldset>
+ <legend>{{ .L.T "users.totpCode" }}</legend>
+ <div data-field>
+ <input x-ref="totpCode" x-model="totpCode" maxlength="6" pattern="[0-9]{6}" placeholder="000000" required />
+ </div>
+ <div class="hstack gap-3">
+ <button type="submit" data-variant="primary">{{ .L.T "globals.buttons.enable" }}</button>
+ <button type="button" class="outline" @click="cancelTotp">{{ .L.T "globals.buttons.cancel" }}</button>
+ </div>
+ </fieldset>
+ </form>
+ </div>
+ </template>
+ </div>
+ {{- end }}
+ </section>
+ {{- end }}
+</section>
+
+<script type="module" src="{{ URI "/admin/static/js/views/profile.js" }}?v={{ .AssetVersion }}" defer></script>
+
+{{ template "admin-footer" . -}}
+{{ end }}