#in validation error & navigating back with validation errors

1 messages · Page 1 of 1 (latest)

mental iris
#
import { createSignal } from 'solid-js';
import { BACKEND_URL } from './env.js';
import { fetcher } from './Helpers/FetchHelper.js'
import { useNavigate } from '@solidjs/router';
// FetchHelper

import { useNavigate } from "@solidjs/router";
import { createStore } from "solid-js/store";

const [store,setStore]=createStore({
    token:null
})


export async function fetcher(url,method='GET',body,headers=null){
    // this will: 
    //      check the store if we already have the bearer token, if not redirect to login,
    //      if we do have the token in the store, we fetch the "/${url}" same as backend, with the method & body:data
    if(store.token==null && document.URL.toString().split('/').at(-1) !== 'login' ){
        const navigate=useNavigate();
        navigate('/login')
    }
    fetch(url,{
        method:method,
        body:body,
        headers: headers ?? { "Content-Type": "application/json" }
    }).then((response)=>{
        if(response.status==401){
            const navigate=useNavigate();
            navigate('/login');
            return;
        }else{
            // save the bearer token in the store
            return response.json()
        }
    }).then((response)=>{
        if(document.URL.toString().split('/'.at(-1)==='login')){
           response.token_type=response.token_type.replace(
                response.token_type[0],response.token_type[0].toUpperCase()
            )
            console.log(response)
            setStore('token',response)
        }
    })
}
#

this is my fetchHelper that i wanna use as an interceptor & fetch function,

but i when i get an unauthorized; i wanna redirect to login with a rejected message in the jsx, should i be setting up a signal for it?

#

it says here partial navigateOptions here, should itsend back the bag into the redirect thing

tough bronze
#

Perhaps nobody has responded to your question because there seems to be an awful lot to unpack here and there also seems to be a bunch of missing context making it difficult to even find a place to start.

mental iris
tough bronze
mental iris
#

nah didn't thought that way, just want fast feedback about the issue, instead of this one back then i wanted more of a detailed inspection & where was i messing it up

#

but thanks for both