Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
get code from api
Browse files Browse the repository at this point in the history
  • Loading branch information
BaseMax committed Sep 12, 2024
1 parent 870009d commit 985e10d
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions script/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const elm_editor_dark = document.querySelector(".editor_dark");
const APP_URL = "https://auth.salamlang.ir";
const APP_URL_VERIFY_TOKEN = APP_URL + "/api/v1/verify_token";
const APP_URL_SAVE = APP_URL + "/api/v1/codes/save";
const APP_URL_GET_CODE = APP_URL + "/api/v1/codes";

// Variables
let token;
Expand Down Expand Up @@ -301,9 +302,8 @@ const save_code = () => {
},
preConfirm: (login) => {
if (login !== "") {
let xhr = new XMLHttpRequest();

xhr.onload = function () {
const xhr = new XMLHttpRequest();
xhr.onload = () => {
if (JSON.parse(xhr.response).status === true) {
Swal.fire({
icon: "success",
Expand Down Expand Up @@ -420,8 +420,7 @@ elm_editor_light.addEventListener("click", () => {

elm_save.addEventListener("click", () => {
if (token !== null) {
let xhr = new XMLHttpRequest();

const xhr = new XMLHttpRequest();
xhr.onload = () => {
if (JSON.parse(xhr.response).status === true) {
save_code();
Expand Down Expand Up @@ -506,7 +505,22 @@ window.addEventListener('load', () => {
const codeParam = new URLSearchParams(window.location.search).get("code");
console.log(codeParam, window.location.search);
if (codeParam !== null) {
elm_code.value = codeParam.trim();
const xhr = new XMLHttpRequest();
xhr.onload = () => {
const obj = JSON.parse(xhr.response);
console.log("Get code: ", obj);
if (obj.status === true) {
elm_code.value = obj.data.code.trim();
} else {
if (localStorage.getItem("cache-code")) {
elm_code.value = localStorage.getItem("cache-code").toString().trim();
}
}
};
xhr.open("GET", APP_URL_GET_CODE);
xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
xhr.setRequestHeader('Authorization', token);
xhr.send();
}
else {
if (localStorage.getItem("cache-code")) {
Expand All @@ -515,7 +529,7 @@ window.addEventListener('load', () => {
}

if (token !== null) {
let xhr = new XMLHttpRequest();
const xhr = new XMLHttpRequest();

xhr.onload = () => {
if (JSON.parse(xhr.response).status === true) {
Expand Down

0 comments on commit 985e10d

Please sign in to comment.