authservice-nextjs
Version:
Next.js SDK for Auth Service - Server and client-side authentication with App Router support
199 lines • 6.78 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NextAuthPages = void 0;
class NextAuthPages {
constructor(auth) {
this.auth = auth;
}
withAuth(getServerSideProps) {
return async (context) => {
const user = await this.auth.getUserFromRequest(context.req);
if (!user) {
return {
redirect: {
destination: this.auth.createRedirectUrl(this.auth['config'].loginUrl || '/login', context.resolvedUrl),
permanent: false,
},
};
}
const enhancedContext = {
...context,
user,
};
if (!getServerSideProps) {
return {
props: { user },
};
}
const result = await getServerSideProps(enhancedContext);
if ('redirect' in result || 'notFound' in result) {
return result;
}
return {
...result,
props: {
...result.props,
user,
},
};
};
}
withPermission(permission, getServerSideProps) {
return async (context) => {
const hasPermission = await this.auth.checkPermissionForRequest(context.req, permission);
if (!hasPermission) {
const user = await this.auth.getUserFromRequest(context.req);
if (!user) {
return {
redirect: {
destination: this.auth.createRedirectUrl(this.auth['config'].loginUrl || '/login', context.resolvedUrl),
permanent: false,
},
};
}
return {
redirect: {
destination: this.auth['config'].unauthorizedUrl || '/unauthorized',
permanent: false,
},
};
}
const user = await this.auth.getUserFromRequest(context.req);
const enhancedContext = {
...context,
user,
};
if (!getServerSideProps) {
return {
props: { user },
};
}
const result = await getServerSideProps(enhancedContext);
if ('redirect' in result || 'notFound' in result) {
return result;
}
return {
...result,
props: {
...result.props,
user,
},
};
};
}
withAnyPermission(permissions, getServerSideProps) {
return async (context) => {
const token = this.auth.getTokenFromRequest(context.req);
if (!token) {
return {
redirect: {
destination: this.auth.createRedirectUrl(this.auth['config'].loginUrl || '/login', context.resolvedUrl),
permanent: false,
},
};
}
const client = this.auth.getClient();
const hasPermission = await client.hasAnyPermission(token, permissions);
if (!hasPermission) {
return {
redirect: {
destination: this.auth['config'].unauthorizedUrl || '/unauthorized',
permanent: false,
},
};
}
const user = await this.auth.getUserFromRequest(context.req);
const enhancedContext = {
...context,
user,
};
if (!getServerSideProps) {
return {
props: { user },
};
}
const result = await getServerSideProps(enhancedContext);
if ('redirect' in result || 'notFound' in result) {
return result;
}
return {
...result,
props: {
...result.props,
user,
},
};
};
}
withAllPermissions(permissions, getServerSideProps) {
return async (context) => {
const token = this.auth.getTokenFromRequest(context.req);
if (!token) {
return {
redirect: {
destination: this.auth.createRedirectUrl(this.auth['config'].loginUrl || '/login', context.resolvedUrl),
permanent: false,
},
};
}
const client = this.auth.getClient();
const hasAllPermissions = await client.hasAllPermissions(token, permissions);
if (!hasAllPermissions) {
return {
redirect: {
destination: this.auth['config'].unauthorizedUrl || '/unauthorized',
permanent: false,
},
};
}
const user = await this.auth.getUserFromRequest(context.req);
const enhancedContext = {
...context,
user,
};
if (!getServerSideProps) {
return {
props: { user },
};
}
const result = await getServerSideProps(enhancedContext);
if ('redirect' in result || 'notFound' in result) {
return result;
}
return {
...result,
props: {
...result.props,
user,
},
};
};
}
checkAuth(getServerSideProps) {
return async (context) => {
const user = await this.auth.getUserFromRequest(context.req);
const enhancedContext = {
...context,
user,
};
if (!getServerSideProps) {
return {
props: { user },
};
}
const result = await getServerSideProps(enhancedContext);
if ('redirect' in result || 'notFound' in result) {
return result;
}
return {
...result,
props: {
...result.props,
user,
},
};
};
}
}
exports.NextAuthPages = NextAuthPages;
//# sourceMappingURL=pages.js.map