Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"aws-sdk": "^2.1399.0",
"axios": "^0.26.1",
"compression": "^1.7.4",
"cookie": "^0.6.0",
"cors": "^2.8.5",
"crypto": "^1.0.1",
"dotenv": "^16.0.0",
Expand Down
3 changes: 3 additions & 0 deletions src/api/UserRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export class UserRouter implements IUserRouter {
router.post('/login', (req, res, next) =>
this._controller.InitLogin(req, res, next).catch(next),
);
router.get('/logout', (req, res, next) =>
this._controller.Logout(req, res, next).catch(next),
);
router.post('/revenuecat', (req, res, next) =>
this._subscription.RevenuecatWebhook(req, res, next).catch(next),
);
Expand Down
16 changes: 10 additions & 6 deletions src/api/middlewares/auth.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { IRequest, IResponse, INext } from '../../interfaces/IRequest';
import JWT from 'jsonwebtoken';
import cookie from 'cookie';
import { APP_CONST } from '../../utils/constant';

const loggedUser = (req: IResponse, _res: IRequest, next: INext) => {
const authorization = req.headers?.authorization;
if (authorization) {
const token = authorization.replace('Bearer', '').trim();
try {
try {
const cookies = cookie.parse(req.headers.cookie || '');
const authorization =
req.headers.authorization || cookies[APP_CONST.SESSION_COOKIE_NAME];
if (authorization) {
const token = authorization.replace('Bearer', '').trim();
const decoded = JWT.verify(token, process.env.APP_SECRET);
req.user = decoded;
} catch (err) {
req.user = undefined;
}
} catch (err) {
req.user = undefined;
}
return next();
};
Expand Down
30 changes: 30 additions & 0 deletions src/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { TYPES } from '../ContainerTypes';
import { IUserService } from '../interfaces/IUserService';
import { IRequest, IResponse } from '../interfaces/IRequest';
import { IUserController } from '../interfaces/IUserController';
import cookie from 'cookie';
import { APP_CONST } from '../utils/constant';

@injectable()
export class UserController implements IUserController {
Expand All @@ -29,6 +31,12 @@ export class UserController implements IUserController {
client_id = await this._userService.getClientID({
origin: origin.replace('https://', '').replace('http://', ''),
});
if (!client_id) {
res
.status(422)
.json({ message: 'Your domain is not registered to use our API' });
return;
}
}
const appleAuth = await this._userService.verifyToken({
token_id,
Expand Down Expand Up @@ -69,9 +77,31 @@ export class UserController implements IUserController {
session: appleAuth.sub,
});

if (!!client_id) {
// is from web enable 2 weeks
const isProd = process.env.NODE_ENV === 'production';
res.setHeader(
'Set-Cookie',
cookie.serialize(APP_CONST.SESSION_COOKIE_NAME, token, {
httpOnly: true,
maxAge: 60 * 60 * 24 * 7 * 2,
sameSite: isProd ? 'none' : null,
secure: isProd,
path: '/',
}),
);
return res.json({ email: user.email });
}
return res.json({ email: user.email, token });
}

public async Logout(req: IRequest, res: IResponse): Promise<IResponse> {
await res.clearCookie(APP_CONST.SESSION_COOKIE_NAME, { path: '/' });
return res.send({
logout: true,
});
}

public async DeleteAccount(
req: IRequest,
res: IResponse,
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/IUserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IRequest, IResponse, INext } from './IRequest';

export interface IUserController {
InitLogin(req: IRequest, res: IResponse, _: INext): Promise<IResponse>;
Logout(req: IRequest, res: IResponse, _: INext): Promise<IResponse>;
getAuth(req: IRequest, res: IResponse, _: INext): Promise<IResponse>;
DeleteAccount(req: IRequest, res: IResponse, _: INext): Promise<IResponse>;
}
6 changes: 0 additions & 6 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { IRouterHttp } from './interfaces/IRouters';
import { TYPES } from './ContainerTypes';
import { handleError } from './api/middlewares/error';
import { IRestClientService } from './interfaces/IRestClientService';
import { ISocketService } from './interfaces/ISocketService';
import { ICacheService } from './interfaces/ICacheService';
import { ILoggerService } from './interfaces/ILoggerService';
import { IVersionMiddleware } from './interfaces/IVersionMiddleware';
import { IResponse, IRequest, INext } from './interfaces/IRequest';
Expand All @@ -20,8 +18,6 @@ import { IResponse, IRequest, INext } from './interfaces/IRequest';
export class Server {
@inject(TYPES.RouterHttp) private _authRouter: IRouterHttp;
@inject(TYPES.RestClientService) private _restClient: IRestClientService;
@inject(TYPES.SocketService) private _socketService: ISocketService;
@inject(TYPES.CacheService) private _cacheService: ICacheService;
@inject(TYPES.LoggerService) private _logger: ILoggerService;
@inject(TYPES.VersionMiddleware) private version: IVersionMiddleware;
run(): void {
Expand All @@ -48,8 +44,6 @@ export class Server {
const httpServer = createServer(app);
httpServer.listen(process.env.API_PORT || 5000, () => {
this._logger.log({ origin: 'init app' });
this._cacheService.connectCacheService();
this._socketService.setupClient(httpServer);
});
}
}
3 changes: 3 additions & 0 deletions src/utils/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const APP_CONST = {
SESSION_COOKIE_NAME: 'bpCookie',
};
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,11 @@ cookie@0.5.0:
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.5.0.tgz#d1f5d71adec6558c58f389987c366aa47e994f8b"
integrity sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==

cookie@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.6.0.tgz#2798b04b071b0ecbff0dbb62a505a8efa4e19051"
integrity sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==

cookie@~0.4.1:
version "0.4.2"
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.2.tgz#0e41f24de5ecf317947c82fc789e06a884824432"
Expand Down