Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. import * as connection from "./connection";
  2.  
  3. export * from "./queries/confirmEmail";
  4. export * from "./queries/resetPassword";
  5. export * from "./queries/user";
  6.  
  7. namespace Sql {
  8. export function query( ...args: any[] ) {
  9. return connection.query( args );
  10. }
  11.  
  12. export class Errors {
  13. static confirmCodeExpired = "45001";
  14. static confirmCodeNonExistent = "45002";
  15. static userAlreadyConfirmed = "45003";
  16. }
  17. }
  18.  
  19. import "../sql";
  20.  
  21. namespace Sql.ResetPassword {
  22. type InsertCodeArgs = {
  23. code: string;
  24. userid: number;
  25. canChange: boolean;
  26. }
  27.  
  28. export function insertCode( args: InsertCodeArgs, cb: Nodeback<void> ) {
  29. Sql.query(
  30. "CALL insert_reset_code( :code, :userid, :canChange );",
  31. {
  32. code: args.code,
  33. userid: args.userid,
  34. canChange: args.canChange ? 1 : 0
  35. },
  36. cb
  37. );
  38. }
  39.  
  40. export function removeCode( code: string, cb: Nodeback<void> ) {
  41. Sql.query(
  42. "DELETE FROM `resetcodes` WHERE `code` = :code;",
  43. { code: code },
  44. cb
  45. );
  46. }
  47.  
  48. export function checkCodeValid( code: string, cb: Nodeback<boolean> ) {
  49. Sql.query(
  50. [
  51. "SELECT NOW() <= `c`.`expires` AS `valid`, `c`.`canchange` as `canchange`, `u`.`id` AS `userid` FROM `resetcodes` AS `c` ",
  52. "INNER JOIN `users` AS `u`",
  53. "ON `u`.`id` = `c`.`userid`",
  54. "WHERE `code` = :code LIMIT 1;"
  55. ].join( " " ),
  56. { code: code },
  57. ( err, data ) => {
  58. if ( err ) return cb( err );
  59. if ( !data || data.length === 0 ) return cb( null, null );
  60.  
  61. return cb( null, data[ 0 ].valid > 0, data[ 0 ].userid, data[ 0 ].canchange > 0 );
  62. }
  63. );
  64. }
  65. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement