Guest User

app.ts

a guest
Jun 15th, 2023
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. import cors from '@koa/cors';
  2. import Koa from 'koa';
  3. import Router from 'koa-router';
  4. import { ApplicationError, errorHandler, isExpectedError } from './helper/error_handler';
  5. import { attachLogger } from './helper/logger';
  6. import { tokenValidation } from './middleware/jwt';
  7. import { apikeyValidation } from './middleware/api_key';
  8. import initializeModules from './modules';
  9. import { Strategy as FacebookStrategy, Profile } from 'passport-facebook';
  10.  
  11. import passport from 'passport'
  12. import { tChannelLink } from './models/channel';
  13.  
  14. const serve = require('koa-static');
  15. const mount = require('koa-mount');
  16.  
  17. const app = new Koa();
  18.  
  19. const router = new Router();
  20.  
  21. passport.use(
  22. new FacebookStrategy(
  23.  
  24. {
  25. clientID: process.env.FACEBOOK_CLIENT_ID,
  26. clientSecret: process.env.FACEBOOK_CLIENT_SECRET,
  27. callbackURL: process.env.CALLBACK_URL,
  28. profileFields: ['id', 'emails', 'name', 'accessToken']
  29. },
  30. async function (accessToken, refreshToken, profile:any, done:any) {
  31.  
  32. console.log("fb stra")
  33.  
  34. const userFB = {
  35. id: profile.id,
  36. full_name: profile.name,
  37. access_token: accessToken,
  38. email: profile.emails && profile.emails.length > 0 ? profile.emails[0].value : '',
  39.  
  40. // Assign other user properties as needed
  41. };
  42.  
  43. console.log(userFB, 'user FB : ')
  44.  
  45. //masukin untuk ke db
  46. //tablenya t_channel_link
  47. console.log(userFB, 'userfB coba')
  48.  
  49. try {
  50. await tChannelLink.query().insert({
  51. channel_id: 'facebook_page',
  52. access_token: userFB.access_token,
  53. is_active: true,
  54. created_by: profile.email,
  55. updated_by: profile.email
  56. });
  57.  
  58. console.log('masuk db tclink')
  59.  
  60. } catch (error) {
  61. return done(error);
  62. }
  63.  
  64. console.log('passed fb stra')
  65.  
  66. return done(null, userFB);
  67. }
  68.  
  69. )
  70. );
  71.  
  72. passport.serializeUser(function (userFB, done) {
  73. done(null, userFB.id);
  74. });
  75.  
  76. passport.deserializeUser(function (id, done) {
  77.  
  78. const userFB = {
  79. id: 'USER_ID',
  80. name: 'John Doe',
  81. email: '[email protected]',
  82. // Other user properties
  83. };
  84.  
  85. done(null, userFB);
  86.  
  87. });
  88.  
  89. app.use(mount('/public', serve('./public')));
  90.  
  91. app.use(cors());
  92.  
  93. app.use(async (ctx, next) => {
  94.  
  95. console.log('ctx pathap', ctx.path)
  96.  
  97. if (ctx.path === '/auth/login/federated/facebook' || ctx.path == '/auth/oauth2/redirect/facebook') {
  98. console.log('harusnya masuk sini')
  99. await next();
  100. } else {
  101. await apikeyValidation(ctx, next);
  102. }
  103. });
  104.  
  105. app.use(tokenValidation);
  106.  
  107. app.use(router.routes());
  108.  
  109. app.use(async (ctx, next) => {
  110. try {
  111. await next();
  112. } catch (err) {
  113. if (err.status === 401) {
  114. err = new ApplicationError({
  115. message: err.message,
  116. type: 'AuthenticationError',
  117. result: null,
  118. error: {
  119. type: "",
  120. message: ""
  121. }
  122. });
  123. }
  124. ctx.app.emit('error', err, ctx);
  125. if (isExpectedError(err)) {
  126. ctx.status = err.httpCode;
  127. ctx.body = {
  128. status: err.status || err.httpCode,
  129. message: err.name,
  130. result: null,
  131. error: {
  132. type: err.status || err.httpCode,
  133. message: err.message,
  134. detail: err.detail
  135. }
  136. };
  137. return;
  138. } else {
  139. ctx.status = 500;
  140.  
  141. let message = 'InternalServerError';
  142.  
  143. if (err.stack) {
  144. message = err.stack.split('\n')[0];
  145. }
  146.  
  147. ctx.body = {
  148. message,
  149. status: false
  150. };
  151. return;
  152. }
  153. }
  154. });
  155.  
  156. app.use(passport.initialize());
  157.  
  158. initializeModules(router);
  159. attachLogger(app);
  160.  
  161. app.use(router.allowedMethods());
  162.  
  163. app.on('error', (err, ctx) => {
  164. errorHandler(err);
  165. });
  166.  
  167. export default app;
  168.  
Advertisement
Add Comment
Please, Sign In to add comment