I am developing a web application using ASP.NET Core 8 for the backend and Vue.js for the frontend.
I set up my project by following the ASP.NET Core app with Vue.js in Visual Studio guide and configured my frontend and backend as separate applications, with the frontend using a proxy setup in vite.config.ts
to communicate with the backend.
Current Implementation
I have implemented authentication by following the guide on using Identity to secure a Web API backend for SPAs, which led me to create custom UI for login.
The Vue.js frontend has separate login pages that interact with the new identity API endpoint.
I have been checking the whether user is logged and checking the protect routes in the frontend as:
router.beforeEach(async (to, from, next) => {
const authStore = useAuthStore();
if (to.matched.some(record => record.meta.requiresAuth)) {
const isSignedIn = await authStore.isSignedIn();
if (!isSignedIn) {
// Redirect to sign-in page if not authenticated
next({ path: '/signin' });
} else {
// Proceed if authenticated
next();
}
} else {
// Route does not require authentication
next();
}
});
To check whether user is already signed in:
async isSignedIn() {
const userInfo = await fetchUserInfo();
const isValid = !!(userInfo && userInfo.email && userInfo.email.length > 0);
this.$patch({
name: userInfo?.email,
isAuthenticated: isValid
})
return isValid;
},
async function fetchUserInfo() {
try {
const response = await fetch('manage/info', {
method: 'GET'
});
if (!response.ok) {
throw new Error('Not authenticated');
}
const data = await response.json();
return data;
} catch (error) {
console.error('Fetch user info error:', error);
return null; // Return null if an error occurs
}
}
What I want to achieve
Instead of using the identity API endpoints, I would like to use the razor pages for user registration and login that can be scaffolded in Visual Studio, which would separate out the logic of authentication from the frontend, and I am little bit doubting on myself that i may not do the authentication flow correct on the frontend.
Also, the new identity endpoints are not easily extended, which doesn't allow the most of the desired field to be added on the different identity endpoints.
My goal is to keep the frontend's UI intact while handling the authentication logic through ASP.NET Core backend pages.
However I do not want to use external authentication providers (e.g., OAuth or IdentityServer).
I want to use only ASP.NET Core Identity for user management and authentication.
Questions
Is it possible to integrate the Razor-based authentication flow while keeping the Vue.js frontend as the main UI framework?
How can I manage redirects and sessions between the Vue.js frontend and the ASP.NET Core backend in this setup?