Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import {
Context, Delete, Get, HttpResponseCreated, HttpResponseNoContent,
HttpResponseNotFound, HttpResponseOK, Post,
ValidateBody, ValidatePathParam
} from '@foal/core';
import { getRepository } from 'typeorm';
import { Todo } from '../entities';
export class ApiController {
@Get('/todos')
async getTodos() {
const todos = await getRepository(Todo).find();
return new HttpResponseOK(todos);
}
@Post('/todos')
@ValidateBody({
// The body request should be an object once parsed by the framework.
// Every additional properties that are not defined in the "properties"
// object should be removed.
additionalProperties: false,
properties: {
// The "text" property of ctx.request.body should be a string if it exists.
text: { type: 'string' }
},
// The property "text" is required.
isAdmin: boolean;
}
const UserModel: Model = model('User', UserSchema);
function AdminRequired() {
return Hook((ctx: Context) => {
if (!ctx.user.isAdmin) {
return new HttpResponseForbidden();
}
});
}
@TokenRequired({ user: fetchUser(UserModel), store: RedisStore })
class MyController {
@Get('/foo')
foo() {
return new HttpResponseOK();
}
@Get('/bar')
@AdminRequired()
bar() {
return new HttpResponseOK();
}
}
class AuthController {
@dependency
store: RedisStore;
@Get('/logout')
class User extends UserWithPermissions {
@Column({ unique: true })
email: string;
@Column()
password: string;
}
@TokenRequired({ store: TypeORMStore, user: fetchUserWithPermissions(User), cookie: true })
class MyController {
@Get('/foo')
foo() {
return new HttpResponseOK();
}
@Get('/bar')
@PermissionRequired('admin')
bar() {
return new HttpResponseOK();
}
}
class AuthController {
@dependency
store: TypeORMStore;
@Get('/logout')
async logout(ctx: Context) {
const response = new HttpResponseNoContent();
await this.store.destroy(ctx.session.sessionID);
removeSessionCookie(response);
return new HttpResponseNoContent();
@LoginRequired({ user: fetchUserWithPermissions(User) })
class MyController {
@Get('/foo')
foo() {
return new HttpResponseOK();
}
@Get('/bar')
@PermissionRequired('admin')
bar() {
return new HttpResponseOK();
}
}
class AuthController {
@Get('/logout')
logout(ctx: Context) {
logOut(ctx);
return new HttpResponseNoContent();
}
@Post('/login')
@ValidateBody({
additionalProperties: false,
properties: {
email: { type: 'string', format: 'email' },
password: { type: 'string' }
},
required: [ 'email', 'password' ],
type: 'object',
})
async login(ctx: Context) {
},
shipDate: {
format: 'date-time',
type: 'string',
},
status: {
description: 'Order Status',
enum: [ 'placed', 'approved', 'delivered' ],
type: 'string',
}
},
type: 'object',
xml: { name: 'Order' }
})
class StoreController {
@Get('/inventory')
@ApiOperation({
description: 'Returns a map of status codes to quantities',
operationId: 'getInventory',
responses: {},
summary: 'Returns pet inventories by status'
})
@ApiResponse(200, {
content: {
'application/json': {
schema: {
additionalProperties: {
format: 'int32',
type: 'integer',
},
type: 'object',
}
@ApiRequestBody({
content: {
'*/*': {
schema: {
items: { $ref: '#/components/schemas/User' },
type: 'array'
}
}
},
description: 'List of user object',
required: true
})
@ApiResponse('default', { description: 'successful operation', content: {} })
createUsersWithListInput() {}
@Get('/login')
@ApiOperation({
operationId: 'loginUser',
responses: {},
summary: 'Logs user into the system'
})
@ApiParameter({
description: 'The user name for login',
in: 'query',
name: 'username',
required: true,
schema: { type: 'string' }
})
@ApiParameter({
description: 'The password for login in clear text',
in: 'query',
name: 'password',
import { Context, Get, LoginRequired, render } from '@foal/core';
import { fetchUserWithPermissions, PermissionRequired } from '@foal/typeorm';
import { User } from '../entities';
@LoginRequired({ user: fetchUserWithPermissions(User), redirect: '/login' })
export class ViewController {
@Get('/')
home(ctx: Context) {
return render('./templates/home.html', {
csrfToken: ctx.request.csrfToken()
}, __dirname);
}
@Get('/admin')
@PermissionRequired('admin', { redirect: '/login' })
admin(ctx: Context) {
return render('./templates/admin.html', {
csrfToken: ctx.request.csrfToken()
}, __dirname);
}
}
export class AuthController {
@dependency
google: GoogleProvider;
@dependency
facebook: FacebookProvider;
@dependency
store: TypeORMStore;
@Get('/signin/google')
redirectToGoogle() {
return this.google.redirect();
}
@Get('/signin/google/cb')
async handleGoogleRedirection(ctx: Context) {
const { userInfo } = await this.google.getUserInfo(ctx);
const session = await this.store.createAndSaveSession({ userInfo });
const response = new HttpResponseRedirect('/');
setSessionCookie(response, session.getToken());
return response;
}
@Get('/signin/facebook')
redirectToFacebook() {
return this.facebook.redirect();
}
@Get('/signin/facebook/cb')
async handleFacebookRedirection(ctx: Context) {
const { userInfo } = await this.facebook.getUserInfo(ctx);
import { AuthController } from './controllers';
export class AppController {
subControllers = [
controller('', AuthController),
];
@Get('/')
@TokenRequired({ cookie: true, store: TypeORMStore, redirectTo: '/signin' })
index(ctx: Context) {
return render('./templates/index.html', {
profile: JSON.stringify(ctx.session.get('profile'))
});
}
@Get('/signin')
signin() {
return render('./templates/signin.html');
}
}
import { Context, controller, Get, render, Session, TokenRequired } from '@foal/core';
import { TypeORMStore } from '@foal/typeorm';
import { AuthController } from './controllers';
export class AppController {
subControllers = [
controller('', AuthController),
];
@Get('/')
@TokenRequired({ cookie: true, store: TypeORMStore, redirectTo: '/signin' })
index(ctx: Context) {
return render('./templates/index.html', {
profile: JSON.stringify(ctx.session.get('profile'))
});
}
@Get('/signin')
signin() {
return render('./templates/signin.html');
}
}