You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.5 KiB

12 months ago
  1. const usersModel = {};
  2. usersModel.tableName = "tbl_members";
  3. /**
  4. * 사용자 데이타 행을 가져온다.
  5. */
  6. usersModel.getUser = async(value, column="id") => {
  7. // 데이타베이스 연결 객체
  8. const db = database();
  9. // 반환할 객체
  10. let result = null
  11. try {
  12. await db(usersModel.tableName)
  13. .select("*")
  14. .select(db.raw("INET_ATON(`loged_ip`) AS `loged_ip`"))
  15. .where(column, value)
  16. .limit(1)
  17. .then((rows) => {
  18. if(rows && rows.length > 0) {
  19. result = rows[0]
  20. }
  21. })
  22. }
  23. catch (e){
  24. result = null;
  25. }
  26. return result;
  27. }
  28. /**
  29. * 사용자 데이타 추가
  30. */
  31. usersModel.addUser = async( data ) => {
  32. // 혹시 빈값이 들어가있는경우 기본값 처리
  33. data.status = data?.status ?? 'Y'
  34. data.login_id = data?.login_id ?? ''
  35. data.login_pass = data?.login_pass ?? ''
  36. data.phone = data?.phone ?? ''
  37. data.nickname = data?.nickname ?? ''
  38. data.auth = data?.auth ?? 1
  39. data.created_at = data?.created_at ?? new Date()
  40. data.updated_at = data?.updated_at ?? new Date()
  41. data.agree_marketing = data?.agree_marketing ?? 'N'
  42. data.privacy_agree_at = data?.privacy_agree_at ?? new Date()
  43. // 비밀번호는 암호화 처리한다.
  44. data.login_pass = require('sha256')(require('md5')(appConfig.secretKey + data.login_pass))
  45. // 결과값을 반환할 flag
  46. let result = false
  47. // 데이타베이스 객체
  48. const db = database()
  49. try {
  50. await db(usersModel.tableName)
  51. .insert(data)
  52. .then(() => {
  53. result = true
  54. })
  55. }
  56. catch {
  57. }
  58. return result;
  59. }
  60. /**
  61. * 토큰을 생성합니다.
  62. */
  63. usersModel.createToken = async(type, userInfo) => {
  64. const jwt = require('jsonwebtoken');
  65. const expiresIn =
  66. type === 'refresh'
  67. ? appConfig.jwt.refreshTokenExpire
  68. : appConfig.jwt.accessTokenExpire
  69. return await jwt.sign({
  70. id: userInfo.id
  71. }, appConfig.secretKey, {
  72. expiresIn
  73. })
  74. }
  75. /**
  76. * 반환용 토큰을 생성합니다.
  77. */
  78. usersModel.responseToken = async(userInfo) => {
  79. let newAccessToken = '',
  80. newRefreshToken = '';
  81. await usersModel.createToken('access', userInfo ).then((v) => (newAccessToken = v));
  82. await usersModel.createToken('refresh', userInfo).then((v) => (newRefreshToken = v));
  83. return {
  84. accessToken: newAccessToken,
  85. refreshToken: newRefreshToken
  86. }
  87. }
  88. module.exports = usersModel;