Move unique OAuth username creation to client-side

Now, on OAuth signup form, we create a unique username with random appended string only if there's a conflict.
Previously, this was always happening during the Slack OAuth flow. This has the benefit of preventing username collisions for all OAuth providers.
This commit is contained in:
Matt Baer 2020-04-22 09:17:25 -04:00
parent 7b7df5535e
commit cf3d5588c2
2 changed files with 16 additions and 5 deletions

View File

@ -13,8 +13,6 @@ package writefreely
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"github.com/writeas/nerds/store"
"github.com/writeas/slug" "github.com/writeas/slug"
"net/http" "net/http"
"net/url" "net/url"
@ -167,7 +165,7 @@ func (c slackOauthClient) inspectOauthAccessToken(ctx context.Context, accessTok
func (resp slackUserIdentityResponse) InspectResponse() *InspectResponse { func (resp slackUserIdentityResponse) InspectResponse() *InspectResponse {
return &InspectResponse{ return &InspectResponse{
UserID: resp.User.ID, UserID: resp.User.ID,
Username: fmt.Sprintf("%s-%s", slug.Make(resp.User.Name), store.GenerateRandomString("0123456789bcdfghjklmnpqrstvwxyz", 5)), Username: slug.Make(resp.User.Name),
DisplayName: resp.User.Name, DisplayName: resp.User.Name,
Email: resp.User.Email, Email: resp.User.Email,
} }

View File

@ -130,7 +130,7 @@ var $aliasSite = document.getElementById('alias-site');
var aliasOK = true; var aliasOK = true;
var typingTimer; var typingTimer;
var doneTypingInterval = 750; var doneTypingInterval = 750;
var doneTyping = function() { var doneTyping = function(genID) {
// Check on username // Check on username
var alias = $alias.el.value; var alias = $alias.el.value;
if (alias != "") { if (alias != "") {
@ -153,6 +153,11 @@ var doneTyping = function() {
$aliasSite.className = $aliasSite.className.replace(/(?:^|\s)error(?!\S)/g, ''); $aliasSite.className = $aliasSite.className.replace(/(?:^|\s)error(?!\S)/g, '');
$aliasSite.innerHTML = '{{ if .Federation }}@<strong>' + data.data + '</strong>@{{.FriendlyHost}}{{ else }}{{.FriendlyHost}}/<strong>' + data.data + '</strong>/{{ end }}'; $aliasSite.innerHTML = '{{ if .Federation }}@<strong>' + data.data + '</strong>@{{.FriendlyHost}}{{ else }}{{.FriendlyHost}}/<strong>' + data.data + '</strong>/{{ end }}';
} else { } else {
if (genID === true) {
$alias.el.value = alias + "-" + randStr(4);
doneTyping();
return;
}
aliasOK = false; aliasOK = false;
$alias.setClass('error'); $alias.setClass('error');
$aliasSite.className = 'error'; $aliasSite.className = 'error';
@ -170,6 +175,14 @@ $alias.on('keyup input', function() {
clearTimeout(typingTimer); clearTimeout(typingTimer);
typingTimer = setTimeout(doneTyping, doneTypingInterval); typingTimer = setTimeout(doneTyping, doneTypingInterval);
}); });
doneTyping(); function randStr(len) {
var res = '';
var chars = '23456789bcdfghjklmnpqrstvwxyz';
for (var i=0; i<len; i++) {
res += chars.charAt(Math.floor(Math.random() * chars.length));
}
return res;
}
doneTyping(true);
</script> </script>
{{end}} {{end}}