import jwtDecode from "jwt-decode" ; import Cookies from "js-cookie" ; import Strapi from "strapi-sdk-javascript/build/main" ; import Lee from "./Lee" ; import Router from "next/router" ; const apiUrl = process . env . API_URL || "http://127.0.0.1" ; const strapi = new Strapi ( apiUrl ) ; export const strapiRegister = ( username , email , password , thumbnail ) => { if ( ! process . browser ) { return undefined ; } strapi . register ( username , email , password ) . then ( ( res ) => { Router . push ( `/congraturation` ) ; unsetToken ( ) ; } ) ; return Promise . resolve ( ) ; } ; export const strapiLogin = ( email , password ) => { if ( ! process . browser ) { return ; } strapi . login ( email , password ) . then ( ( res ) => { setToken ( res ) ; } ) ; return Promise . resolve ( ) ; } ; export const setToken = ( token ) => { if ( ! process . browser ) { return ; } Cookies . set ( "username" , token . user . username , { secure : true } ) ; Cookies . set ( "jwt" , token . jwt , { secure : true } ) ; if ( Cookies . get ( "jwt" ) ) { location . reload ( ) ; setTimeout ( ( ) => { Lee . loadingFinish ( ) ; } , 100 ) ; } } ; export const unsetToken = ( ) => { if ( ! process . browser ) { return ; } Cookies . remove ( "jwt" ) ; Cookies . remove ( "username" ) ; window . localStorage . setItem ( "logout" , Date . now ( ) ) ; } ; export const getUserFromServerCookie = ( req ) => { if ( ! req . headers . cookie || "" ) { return undefined ; } let username = req . headers . cookie . split ( ";" ) . find ( ( user ) => user . trim ( ) . startsWith ( "username=" ) ) ; if ( username ) { username = username . split ( "=" ) [ 1 ] ; } const jwtCookie = req . headers . cookie . split ( ";" ) . find ( ( c ) => c . trim ( ) . startsWith ( "jwt=" ) ) ; if ( ! jwtCookie ) { return undefined ; } const jwt = jwtCookie . split ( "=" ) [ 1 ] ; return jwtDecode ( jwt ) , username ; } ; export const getUserFromLocalCookie = ( ) => { return Cookies . get ( "username" ) ; } ; const getQueryParams = ( ) => { const params = { } ; window . location . href . replace ( /([^(?|#)=&]+)(=([^&]*))?/g , ( $0 , $1 , $2 , $3 ) => { params [ $1 ] = $3 ; } ) ; return params ; } ; export const extractInfoFromHash = ( ) => { if ( ! process . browser ) { return undefined ; } const { id_token , state } = getQueryParams ( ) ; return { token : id_token , secret : state } ; } ;