Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from flask import request, current_app 

2from flask_restx import Resource 

3from flask_jwt_extended import get_raw_jwt, jwt_required 

4import asyncio 

5from threading import Thread 

6 

7from src.utils import validation_error 

8 

9# Auth modules 

10from src.service import AuthService, ExternalService 

11from src.dto import AuthDto 

12from src.schemas import LoginSchema, RegisterSchema, ResetSchema, ForgetSchema 

13 

14api = AuthDto.api 

15auth_success = AuthDto.auth_success 

16 

17login_schema = LoginSchema() 

18register_schema = RegisterSchema() 

19reset_schema = ResetSchema() 

20forget_schema = ForgetSchema() 

21 

22 

23@api.route("/login") 

24class AuthLogin(Resource): 

25 """ User login endpoint 

26 User registers then receives the user's information and access_token 

27 """ 

28 

29 auth_login = AuthDto.auth_login 

30 

31 @api.doc( 

32 "Auth login", 

33 responses={ 

34 200: ("Logged in", auth_success), 

35 401: "Failed to log in.", 

36 }, 

37 ) 

38 @api.doc(security=None) 

39 @api.expect(auth_login, validate=True) 

40 def post(self): 

41 """ Login using email and password """ 

42 # Grab the json data 

43 login_data = request.get_json() 

44 

45 # Validate data 

46 if (errors := login_schema.validate(login_data)): 

47 return validation_error(False, errors) 

48 

49 res,code = AuthService.login(login_data) 

50 

51 if res['status']: 

52 thread_spotify = Thread(target=ExternalService.get_spotify_data, args=(res['user']['uuid'],current_app._get_current_object())) 

53 thread_spotify.daemon = True 

54 thread_spotify.start() 

55 

56 thread_tmdb = Thread(target=ExternalService.get_tmdb_data, args=(res['user']['uuid'],current_app._get_current_object())) 

57 thread_tmdb.daemon = True 

58 thread_tmdb.start() 

59 

60 thread_tmdb = Thread(target=ExternalService.get_gbooks_data, args=(res['user']['uuid'],current_app._get_current_object())) 

61 thread_tmdb.daemon = True 

62 thread_tmdb.start() 

63 

64 return res,code 

65 

66 

67@api.route("/register") 

68class AuthRegister(Resource): 

69 """ User register endpoint 

70 User registers then receives the user's information and access_token 

71 """ 

72 

73 auth_register = AuthDto.auth_register 

74 

75 @api.doc( 

76 "Auth registration", 

77 responses={ 

78 201: ("Successfully registered user.", auth_success), 

79 400: "Malformed data or validations failed.", 

80 }, 

81 ) 

82 @api.doc(security=None) 

83 @api.expect(auth_register, validate=True) 

84 def post(self): 

85 """ User registration """ 

86 # Grab the json data 

87 register_data = request.get_json() 

88 

89 # Validate data 

90 if (errors := register_schema.validate(register_data)): 

91 return validation_error(False, errors) 

92 

93 return AuthService.register(register_data) 

94 

95 

96@api.route("/logout") 

97class AuthLogout(Resource): 

98 """ User logout endpoint 

99 """ 

100 @api.doc( 

101 "Auth logout", 

102 responses={ 

103 204: (""), 

104 401: ("Authentication required"), 

105 }, 

106 ) 

107 @jwt_required 

108 def post(self): 

109 """ User logout """ 

110 token = get_raw_jwt() 

111 

112 return AuthService.logout(token) 

113 

114 

115@api.route("/forget") 

116class AuthForgotPassword(Resource): 

117 auth_forgot = AuthDto.auth_forgot 

118 """ User password forgot """ 

119 @api.doc( 

120 "Auth password forgot", 

121 responses={ 

122 204: ("Successfully reset mail sent."), 

123 }, 

124 ) 

125 @api.doc(security=None) 

126 @api.expect(auth_forgot, validate=True) 

127 def post(self): 

128 """ User password forgot """ 

129 data = request.get_json() 

130 # Validate data 

131 if (errors := forget_schema.validate(data)): 

132 return validation_error(False, errors) 

133 return AuthService.forget(data['email']) 

134 

135 

136@api.route("/reset") 

137class AuthResetPassword(Resource): 

138 auth_reset = AuthDto.auth_reset 

139 """ User password reset """ 

140 @api.doc( 

141 "Auth password reset", 

142 responses={ 

143 204: ("Successfully reset password"), 

144 }, 

145 ) 

146 @api.doc(security=None) 

147 @api.expect(auth_reset, validate=True) 

148 def post(self): 

149 """ User password reset""" 

150 data = request.get_json() 

151 # Validate data 

152 if (errors := reset_schema.validate(data)): 

153 return validation_error(False, errors) 

154 return AuthService.reset(data)