Skip to content

Commit

Permalink
correctly redirect after login and signup
Browse files Browse the repository at this point in the history
  • Loading branch information
jpelay committed Nov 12, 2024
1 parent ca1a343 commit 69598af
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 23 deletions.
2 changes: 1 addition & 1 deletion static/js/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ let theAdventures: Record<string, Adventure> = {};
export let theLevel: number = 0;
export let theLanguage: string = '';
export let theKeywordLanguage: string = 'en';
export let theDomainName: string = '';
let theStaticRoot: string = '';
let currentTab: string;
let theUserIsLoggedIn: boolean;
let selectedURI: JQuery<HTMLElement>;
let theDomainName: string = '';
//create a synth and connect it to the main output (your speakers)
//const synth = new Tone.Synth().toDestination();

Expand Down
25 changes: 16 additions & 9 deletions static/js/appbundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -60665,6 +60665,7 @@ ${o3}` : i3;
store_parsons_attempt: () => store_parsons_attempt,
submit_program: () => submit_program,
success: () => success,
theDomainName: () => theDomainName,
theGlobalDebugger: () => theGlobalDebugger,
theGlobalEditor: () => theGlobalEditor,
theGlobalSourcemap: () => theGlobalSourcemap,
Expand Down Expand Up @@ -120041,17 +120042,21 @@ def note_with_error(value, err):
tryCatchPopup(async () => {
const body = convertFormJSON($(this));
await postNoResponse("/auth/signup", body);
afterLogin({ "first_time": true, "is_teacher": "is_teacher" in body });
afterLogin({ "first_time": true, "is_teacher": "is_teacher" in body, "lang": body["lang"] });
});
});
$("form#login").on("submit", function(e) {
e.preventDefault();
tryCatchPopup(async () => {
const response = await postJson("/auth/login", convertFormJSON($(this)));
if (response["first_time"]) {
return afterLogin({ "first_time": true });
return afterLogin({ "first_time": true, "lang": response["lang"] });
}
return afterLogin({ "admin": response["admin"] || false, "teacher": response["teacher"] });
return afterLogin({
"admin": response["admin"] || false,
"teacher": response["teacher"] || false,
"lang": response["lang"]
});
});
});
$("form#profile").on("submit", function(e) {
Expand Down Expand Up @@ -120176,8 +120181,10 @@ def note_with_error(value, err):
async function afterLogin(loginData) {
var _a3;
const { url } = (_a3 = localLoadOnce(REDIRECT_AFTER_LOGIN_KEY)) != null ? _a3 : {};
const userLang = loginData["lang"];
if (url && !loginData["first_time"]) {
window.location = url;
const urlObject = new URL(url);
window.location.href = `${urlObject.protocol}//${userLang}.${theDomainName}${urlObject.pathname}${urlObject.search !== "" ? "?" + urlObject.search : ""}`;
return;
}
const joinClassString = localStorage.getItem("hedy-join");
Expand All @@ -120187,15 +120194,15 @@ def note_with_error(value, err):
return join_class(joinClass.id, joinClass.name);
}
if (loginData["first_time"] && loginData["is_teacher"]) {
return redirect("for-teachers");
window.location.href = `${location.protocol}//${userLang}.${theDomainName}/for-teachers`;
} else if (loginData["first_time"] && !loginData["is_teacher"]) {
return redirect("hedy/1");
window.location.href = `${location.protocol}//${userLang}.${theDomainName}/hedy/1`;
}
if (loginData["admin"]) {
return redirect("admin");
window.location.href = `${location.protocol}//${userLang}.${theDomainName}/admin`;
}
if (loginData["teacher"]) {
return redirect("for-teachers");
window.location.href = `${location.protocol}//${userLang}.${theDomainName}/for-teachers`;
}
redirect("");
}
Expand Down Expand Up @@ -120567,11 +120574,11 @@ def note_with_error(value, err):
var theLevel = 0;
var theLanguage2 = "";
var theKeywordLanguage = "en";
var theDomainName = "";
var theStaticRoot = "";
var currentTab;
var theUserIsLoggedIn;
var selectedURI;
var theDomainName = "";
var synth = new PolySynth(Synth).toDestination();
var slides_template = `
<!DOCTYPE html>
Expand Down
4 changes: 2 additions & 2 deletions static/js/appbundle.js.map

Large diffs are not rendered by default.

23 changes: 15 additions & 8 deletions static/js/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { modal, tryCatchPopup } from './modal';
import { join_class } from './teachers';
import { localLoadOnce, localSave } from './local';
import { postNoResponse, postJson } from './comm';
import { theDomainName } from './app';

const REDIRECT_AFTER_LOGIN_KEY = 'login-redirect';

Expand Down Expand Up @@ -93,7 +94,7 @@ export function initializeFormSubmits() {
tryCatchPopup(async () => {
const body = convertFormJSON($(this))
await postNoResponse('/auth/signup', body);
afterLogin({"first_time": true, "is_teacher": "is_teacher" in body});
afterLogin({"first_time": true, "is_teacher": "is_teacher" in body, "lang": body["lang"]});
});
});

Expand All @@ -102,9 +103,13 @@ export function initializeFormSubmits() {
tryCatchPopup(async () => {
const response = await postJson('/auth/login', convertFormJSON($(this)));
if (response['first_time']) {
return afterLogin({"first_time": true});
return afterLogin({"first_time": true, "lang": response["lang"]});
}
return afterLogin({"admin": response['admin'] || false, "teacher": response['teacher']} || false);
return afterLogin({
"admin": response['admin'] || false,
"teacher": response['teacher'] || false,
"lang": response["lang"]
});
});
});

Expand Down Expand Up @@ -249,8 +254,10 @@ export function update_user_tags() {
*/
async function afterLogin(loginData: Dict<boolean>) {
const { url } = localLoadOnce(REDIRECT_AFTER_LOGIN_KEY) ?? {};
const userLang = loginData['lang']
if (url && !loginData['first_time']) {
window.location = url;
const urlObject = new URL(url);
window.location.href = `${urlObject.protocol}//${userLang}.${theDomainName}${urlObject.pathname}${urlObject.search !== "" ? "?" + urlObject.search : ''}`;
return;
}

Expand All @@ -263,19 +270,19 @@ async function afterLogin(loginData: Dict<boolean>) {

// If the user logs in for the first time and is a teacher -> redirect to the for teacher page
if (loginData['first_time'] && loginData['is_teacher']) {
return redirect('for-teachers');
window.location.href = `${location.protocol}//${userLang}.${theDomainName}/for-teachers`
// If it's a student, send him to the first level
} else if(loginData['first_time'] && !loginData['is_teacher']) {
return redirect('hedy/1')
window.location.href = `${location.protocol}//${userLang}.${theDomainName}/hedy/1`
}
// If the user is an admin -> re-direct to admin page after login
if (loginData['admin']) {
return redirect('admin');
window.location.href = `${location.protocol}//${userLang}.${theDomainName}/admin`
}

// If the user is a teacher -> re-direct to for-teachers page after login
if (loginData['teacher']) {
return redirect('for-teachers');
window.location.href = `${location.protocol}//${userLang}.${theDomainName}/for-teachers`
}
// Otherwise, redirect to the programs page
redirect('');
Expand Down
2 changes: 1 addition & 1 deletion website/auth_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def login(self, language='en'):
if user.get("teacher") and user.get("verification_pending"):
self.db.update_user(user["username"], {"verification_pending": None})
resp_body = {"first_time": True}

resp_body['lang'] = user.get('language', 'en')
resp = make_response(resp_body)
# We set the cookie to expire in a year,
# just so that the browser won't invalidate it if the same cookie gets renewed by constant use.
Expand Down
4 changes: 2 additions & 2 deletions website/for_teachers.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,9 +666,9 @@ def preview_teacher_mode(self, language="en"):
return redirect("/for-teachers")

@route("/exit-preview-teacher-mode", methods=["GET"])
@route("/exit-preview-teacher-mode", methods=["GET"], subdomain="<subdomaon>")
@route("/exit-preview-teacher-mode", methods=["GET"], subdomain="<language>")
# Note: we explicitly do not need login here, anyone can exit preview mode
def exit_teacher_mode(self):
def exit_teacher_mode(self, language="en"):
self.auth.logout()
return redirect("/hedy")

Expand Down

0 comments on commit 69598af

Please sign in to comment.