我想在Sveltekit中设置jwt cookie应用程序从一个GolangAPI。下面的代码可以成功运行,但是浏览器中没有设置cookie。是否不可能从外部API设置jwt cookie ?
我的大部分工作都是在外部API中完成的,这就是为什么我想从相同的API进行身份验证。
在Sveltekit代码中,
async function OnSubmit() {
//test
let res = await fetch('http://localhost:50000/login', {
credentials: 'same-origin',
method: 'POST',
mode: 'cors',
body: JSON.stringify({
Username: 'raka',
Password: 'password1'
})
});
console.log('🚀 ~ file: index@blank.svelte ~ line 45 ~ OnSubmit ~ res', res);
if (res.ok) {
console.log('🚀 ~ file: index.svelte ~ line 49 ~ OnSubmit ~ value : ', 200);
}
}
在Golang API中,
func main() {
http.HandleFunc("/login", handler.Login)
http.HandleFunc("/home", handler.Home)
http.HandleFunc("/", handler.Home2)
http.HandleFunc("/refresh", handler.Refresh)
fmt.Println("listening in port 50000")
log.Fatal(http.ListenAndServe(":50000", nil))
}
func Login(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
fmt.Println("Login is hit ...")
var credentials Credentials
err := json.NewDecoder(r.Body).Decode(&credentials)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
expectedPassword, ok := Users[credentials.Username]
if !ok || expectedPassword != credentials.Password {
w.WriteHeader(http.StatusUnauthorized)
return
}
expirationTime := time.Now().Add(time.Minute * 5)
claims := &Claims{
Username: credentials.Username,
StandardClaims: jwt.StandardClaims{
ExpiresAt: expirationTime.Unix(),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
tokenString, err := token.SignedString(jwtKey)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
http.SetCookie(w, &http.Cookie{
Name: "token",
Value: tokenString,
Expires: expirationTime,
})
}
使用钩子,下面的代码应该可以工作
在hooks.js:
import {serialize } from 'cookie'; //pnpm install cookie
export async function handle({ event, resolve }) {
if (event.url.pathname.startsWith('/login')) {
let res = await fetch('http://localhost:50000/login', {
credentials: 'same-origin',
method: 'POST',
mode: 'cors',
body: JSON.stringify({
Username: 'raka',
Password: 'password1'
})
});
if (res.ok) {
let tokenCookie = serialize('jwt', jwt, {
path: '/',
httpOnly: true,
sameSite: 'strict',
secure: true,
maxAge: 60 * 60, // 1hr
})
let response = new Response(JSON.stringify({}));
response.headers.set('Set-Cookie', tokenCookie);
return response;
}
}
}