I have app.ts file:
import express, { Express } from "express";
import cors from "cors";
import cookieParser from "cookie-parser";
//
import userRouter from "./routes/userRoutes";
import cardsRouter from "./routes/cardsRoutes";
import contactsRouter from "./routes/contactsRoutes";
import transferRouter from "./routes/transferRoutes";
import authRouter from "./routes/authRoutes";
//
const app: Express = express();
app.use(express.json());
app.use(cookieParser());
app.use(
cors({
credentials: true,
origin: "http://localhost:4200",
})
);
app.get("/", (req, res) => {
res
.status(202)
.cookie("Name", "testCookie", {
sameSite: "strict",
path: "/",
expires: new Date(new Date().getTime() + 100 * 1000),
httpOnly: true,
secure: false,
})
.send("success!");
});
app.use("/api/v1", [
userRouter,
cardsRouter,
contactsRouter,
transferRouter,
authRouter,
]);
export default app;
and service:
import { Injectable } from '@angular/core';
import { CookieService } from 'ngx-cookie-service';
import axios from 'axios';
axios.defaults.withCredentials = true;
@Injectable({
providedIn: 'root',
})
export class AuthService {
constructor(private cookieService: CookieService) {}
url = 'http://localhost:8000/api/v1/login';
token: string = '';
async login(formObject: any) {
try {
await axios
.get(`${this.url}`, { withCredentials: true })
.then((res) => console.log(res));
console.log('http cookie set!');
} catch (error) {
console.error(error);
}
}
}
login route leads to this controller:
export const checkCookie = async (req: Request, res: Response) => {
res.status(200).json({ message: "Hello" });
};
when i click login theres no error, but i get no cookie either. I check it in mozilla devtools -> storage -> cookies, and there are test cookies, but no http only cookie. What is wrong? The server runs of 8000 port and frontend od 4200.