Instantly share code, notes, and snippets.

@idkjs /Auth.re Secret
Last active Jan 31, 2019

Embed
What would you like to do?
converting auth.js to reasonml
const isBrowser = typeof window !== `undefined`
export const setUser = user => (window.localStorage.gatsbyUser = JSON.stringify(user))
const getUser = () =>
window.localStorage.gatsbyUser
? JSON.parse(window.localStorage.gatsbyUser)
: {}
export const isLoggedIn = () => {
if (!isBrowser) return false
const user = getUser()
if (user) return !!user.username
}
export const getCurrentUser = () => isBrowser && getUser()
export const logout = callback => {
if (!isBrowser) return
setUser({})
callback()
}
[@bs.val] external browserEnv: bool = "process.browser";
let isBrowser = browserEnv;
Js.log2("isBrowser: ", isBrowser);
/* {"email_verified":true,"phone_number_verified":false,"phone_number":"+5555555555","email":"aarmand.inbox@gmail.com","username":"idkjs"} */
let getUser = () => {
let user = Dom.Storage.(localStorage |> getItem("gatsbyUser"));
let result =
switch (user) {
| None => ""
| Some(user) => user
};
Js.log2("result", result);
};
getUser();
let getUser = () => {
let user = Dom.Storage.(localStorage |> getItem("gatsbyUser"));
let result = switch user {
| None => ""
| Some(user) => user
}
Js.log(result);
result;
};
let user = getUser();
// returns
// {"email_verified":true,"phone_number_verified":false,"phone_number":"+5555555555","email":"aarmand.inbox@gmail.com","username":"idkjs"}
Js.log2("USER",user);
let isLoggedIn = (isBrowser, user) => {
let user = getUser();
switch(isBrowser, user) {
|(false,_) => ()
|(true, user) => {
switch user {
| None => ""
| Some(u) => u
}}
}
};
let getCurrentUser = () => if (isBrowser) {
let user = getUser();
/* return the user */
user;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment