-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Catch 401 errors and make refresh token
- Loading branch information
1 parent
ffe5328
commit b88c947
Showing
7 changed files
with
183 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Buffer } from "buffer"; | ||
import Cookies from "js-cookie"; | ||
|
||
/** | ||
* @name getSessionData | ||
* @method | ||
* @private | ||
* @return {Object} session data | ||
*/ | ||
export function getUserSessionData() { | ||
let sessionData = {}; | ||
try { | ||
const data = Cookies.get("storefront-session"); | ||
const expanded = Buffer.from(data, "base64").toString("utf8"); | ||
sessionData = JSON.parse(expanded, (key, value) => { | ||
if (key === "user" && typeof value === "string") return JSON.parse(value); | ||
return value; | ||
}); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error(`Error while reading session data: ${error}`); | ||
} | ||
if (typeof sessionData.passport !== "object") { | ||
return {}; | ||
} | ||
return sessionData.passport.user; | ||
} | ||
|
||
/** | ||
* @name getOAuthClientConfig | ||
* @method | ||
* @private | ||
* @return {Object} client config data | ||
*/ | ||
export function getOAuthClientConfig() { | ||
let clientConfig = {}; | ||
try { | ||
const data = Cookies.get("oauth_client_config"); | ||
const expanded = Buffer.from(data, "base64").toString("utf8"); | ||
clientConfig = JSON.parse(expanded); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error(`Error while reading session data: ${error}`); | ||
} | ||
return clientConfig; | ||
} | ||
|
||
/** | ||
* @name createNewSessionData | ||
* @method | ||
* @private | ||
* @param {Object} json session data | ||
* @return {String} encoded string containing new session info | ||
*/ | ||
export function createNewSessionData(json) { | ||
let newSessionInfo = ""; | ||
try { | ||
newSessionInfo = Buffer.from(JSON.stringify({ | ||
passport: { | ||
user: { | ||
accessToken: json.access_token, | ||
refreshToken: json.refresh_token | ||
} | ||
} | ||
})).toString("base64"); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error("Error writing new session info", error); | ||
} | ||
return newSessionInfo; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters