#HOW TO MANAGES COOKIES
3 messages · Page 1 of 1 (latest)
func main() {
fmt.Println(":rocket: server is running ")
r := http.NewServeMux()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
u := user{"session!"}
c := http.Cookie{Name: "session", Value: "value of cookie !", Path: "/",}
w.WriteHeader(http.StatusOK)
http.SetCookie(w, &c)
w.Header().Set("Content-type", "application/json")
json.NewEncoder(w).Encode(&u)
})
log.Fatal(http.ListenAndServe(":3000", r))
}
client code : ```jsx
import { cookies } from 'next/headers';
interface user {
name: string;
}
const FetchApi = async (): Promise<user> => {
const data = await fetch("http://localhost:3000/", {cache:"no-store"});
const response = data.json();
const cookieStore = cookies();
console.log("Does we have a session ? : ", cookieStore.get("session"))
return response;
};
const Home = async () => {
const { name } = await FetchApi();
return (
<>
<div>
{name}
</div>
</>
);
};
export default Home;