mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-02-05 21:33:40 +08:00
fixed account page not loading properly,
added sign in with google, added name check
This commit is contained in:
parent
e0f64a4e2c
commit
355517ac53
5 changed files with 107 additions and 6 deletions
|
@ -1,3 +1,7 @@
|
|||
import { db_updateName } from "./db";
|
||||
|
||||
var gmailProvider = new firebase.auth.GoogleAuthProvider();
|
||||
|
||||
function showSignOutButton() {
|
||||
$(".signOut").removeClass("hidden").css("opacity", 1);
|
||||
}
|
||||
|
@ -48,6 +52,40 @@ function signIn() {
|
|||
}
|
||||
}
|
||||
|
||||
async function signInWithGoogle() {
|
||||
$(".pageLogin .preloader").removeClass("hidden");
|
||||
|
||||
if ($(".pageLogin .login #rememberMe input").prop("checked")) {
|
||||
//remember me
|
||||
await firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL);
|
||||
firebase
|
||||
.auth()
|
||||
.signInWithPopup(gmailProvider)
|
||||
.then((result) => {
|
||||
console.log(result);
|
||||
})
|
||||
.catch((error) => {
|
||||
Notifications.add(error.message, -1);
|
||||
$(".pageLogin .preloader").addClass("hidden");
|
||||
});
|
||||
} else {
|
||||
//dont remember
|
||||
await firebase
|
||||
.auth()
|
||||
.setPersistence(firebase.auth.Auth.Persistence.SESSION);
|
||||
firebase
|
||||
.auth()
|
||||
.signInWithPopup(gmailProvider)
|
||||
.then((result) => {
|
||||
console.log(result);
|
||||
})
|
||||
.catch((error) => {
|
||||
Notifications.add(error.message, -1);
|
||||
$(".pageLogin .preloader").addClass("hidden");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let dontCheckUserName = false;
|
||||
|
||||
function signUp() {
|
||||
|
@ -270,10 +308,46 @@ firebase.auth().onAuthStateChanged(function (user) {
|
|||
|
||||
function getAccountDataAndInit() {
|
||||
db_getUserSnapshot()
|
||||
.then((e) => {
|
||||
if (db_getSnapshot() === null) {
|
||||
.then(async (e) => {
|
||||
let snap = db_getSnapshot();
|
||||
if (snap === null) {
|
||||
throw "Missing db snapshot. Client likely could not connect to the backend.";
|
||||
}
|
||||
let user = firebase.auth().currentUser;
|
||||
if (snap.name === undefined) {
|
||||
//verify username
|
||||
if (Misc.isUsernameValid(user.displayName)) {
|
||||
//valid, just update
|
||||
snap.name = user.displayName;
|
||||
db_setSnapshot(snap);
|
||||
db_updateName(user.uid, user.displayName);
|
||||
} else {
|
||||
//invalid, get new
|
||||
Notifications.add("Invalid name", 0);
|
||||
let promptVal = null;
|
||||
let cdnVal = undefined;
|
||||
|
||||
while (
|
||||
promptVal === null ||
|
||||
cdnVal === undefined ||
|
||||
cdnVal.data.status < 0
|
||||
) {
|
||||
promptVal = prompt(
|
||||
"Your name is either invalid or unavailable (you also need to do this if you used Google Sign Up). Please provide a new display name (cannot be longer than 14 characters, can only contain letters, numbers, underscores, dots and dashes):"
|
||||
);
|
||||
cdnVal = await CloudFunctions.changeDisplayName({
|
||||
uid: user.uid,
|
||||
name: promptVal,
|
||||
});
|
||||
if (cdnVal.data.status === 1) {
|
||||
Notifications.add("Name updated", 1);
|
||||
location.reload();
|
||||
} else if (cdnVal.data.status < 0) {
|
||||
Notifications.add(cdnVal.data.message, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!configChangedBeforeDb) {
|
||||
if (cookieConfig === null) {
|
||||
accountIconLoading(false);
|
||||
|
@ -363,7 +437,10 @@ function getAccountDataAndInit() {
|
|||
// ) {
|
||||
// config.resultFilters.funbox = defaultAccountFilters.funbox;
|
||||
// }
|
||||
if ($(".pageLogin").hasClass("active")) {
|
||||
if (
|
||||
$(".pageLogin").hasClass("active") ||
|
||||
window.location.pathname === "/account"
|
||||
) {
|
||||
changePage("account");
|
||||
}
|
||||
refreshThemeButtons();
|
||||
|
@ -2415,11 +2492,16 @@ $(".pageLogin .login input").keyup((e) => {
|
|||
}
|
||||
});
|
||||
|
||||
$(".pageLogin .login .button").click((e) => {
|
||||
$(".pageLogin .login .button.signIn").click((e) => {
|
||||
configChangedBeforeDb = false;
|
||||
signIn();
|
||||
});
|
||||
|
||||
$(".pageLogin .login .button.signInWithGoogle").click((e) => {
|
||||
configChangedBeforeDb = false;
|
||||
signInWithGoogle();
|
||||
});
|
||||
|
||||
$(".signOut").click((e) => {
|
||||
signOut();
|
||||
});
|
||||
|
|
|
@ -5,6 +5,10 @@ db.settings({ experimentalForceLongPolling: true });
|
|||
|
||||
let dbSnapshot = null;
|
||||
|
||||
export function db_updateName(uid, name) {
|
||||
db.collection(`users`).doc(uid).set({ name: name }, { merge: true });
|
||||
}
|
||||
|
||||
export function db_getSnapshot() {
|
||||
return dbSnapshot;
|
||||
}
|
||||
|
|
|
@ -566,3 +566,12 @@ export function cleanTypographySymbols(textToClean) {
|
|||
};
|
||||
return textToClean.replace(/[“”’‘—,…«»–]/g, (char) => specials[char] || "");
|
||||
}
|
||||
|
||||
export function isUsernameValid(name) {
|
||||
if (name === null || name === undefined || name === "") return false;
|
||||
if (/miodec/.test(name.toLowerCase())) return false;
|
||||
if (/bitly/.test(name.toLowerCase())) return false;
|
||||
if (name.length > 14) return false;
|
||||
if (/^\..*/.test(name.toLowerCase())) return false;
|
||||
return /^[0-9a-zA-Z_.-]+$/.test(name);
|
||||
}
|
||||
|
|
|
@ -5690,9 +5690,10 @@ $(document).ready(() => {
|
|||
history.replaceState("/", null, "/");
|
||||
}
|
||||
} else if (window.location.pathname === "/account") {
|
||||
history.replaceState("/", null, "/");
|
||||
// history.replaceState("/", null, "/");
|
||||
} else if (/challenge_.+/g.test(window.location.pathname)) {
|
||||
//do nothing
|
||||
// }
|
||||
} else if (window.location.pathname !== "/") {
|
||||
let page = window.location.pathname.replace("/", "");
|
||||
changePage(page);
|
||||
|
|
|
@ -3151,10 +3151,15 @@
|
|||
Remember me
|
||||
</label>
|
||||
</div>
|
||||
<div class="button">
|
||||
<div class="button signIn">
|
||||
<i class="fas fa-sign-in-alt"></i>
|
||||
Sign In
|
||||
</div>
|
||||
<div style="font-size: 0.75rem; text-align: center">or</div>
|
||||
<div class="button signInWithGoogle">
|
||||
<i class="fab fa-google"></i>
|
||||
Google Sign In
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
|
Loading…
Reference in a new issue