LoginPage.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <template>
  2. <q-page>
  3. <div style="height: 82px"></div>
  4. <div class="row q-mb-xl justify-center">
  5. <div
  6. class="col-10 col-sm-5 col-md-3 rounded-borders login-border shadow-7"
  7. >
  8. <div class="text-h5 text-left text-weight-bold text-black q-pa-md">
  9. Login
  10. </div>
  11. <div class="col-12 q-pa-md">
  12. <q-input
  13. :rules="[(val) => !!val || 'Field is required']"
  14. color="blue-12"
  15. v-model="username"
  16. label="Enter your email"
  17. ref="usernameInput"
  18. >
  19. <template v-slot:prepend>
  20. <q-icon name="account_circle" />
  21. </template>
  22. </q-input>
  23. </div>
  24. <div class="col-12 q-pa-md">
  25. <q-input
  26. :rules="[(val) => !!val || 'Field is required']"
  27. color="blue-12"
  28. v-model="password"
  29. type="password"
  30. label="Enter your password"
  31. ref="passwordInput"
  32. >
  33. <template v-slot:prepend>
  34. <q-icon name="lock" />
  35. </template>
  36. </q-input>
  37. </div>
  38. <div class="col-12 q-pa-md">
  39. <q-btn color="primary" @click="login()" label="Login" />
  40. <q-btn
  41. icon="lab la-facebook"
  42. color="blue-6"
  43. dense
  44. rounded
  45. class="q-ma-sm"
  46. @click="goToFacebookLogin"
  47. />
  48. </div>
  49. </div>
  50. </div>
  51. </q-page>
  52. </template>
  53. <script>
  54. import { ref, onMounted, onBeforeMount, getCurrentInstance } from "vue";
  55. import { useQuasar } from "quasar";
  56. import { api } from "boot/axios";
  57. import Qs from "qs";
  58. export default {
  59. name: "LoginPage",
  60. setup() {
  61. const instance = getCurrentInstance();
  62. const app = getCurrentInstance().appContext.app;
  63. const gp = app.config.globalProperties;
  64. const $q = useQuasar();
  65. const username = ref("");
  66. const password = ref("");
  67. function login() {
  68. const isValidPassword = instance.refs.passwordInput.validate();
  69. const isValidUsername = instance.refs.usernameInput.validate();
  70. if (isValidUsername && isValidPassword) {
  71. // do real login logic
  72. var data = {
  73. username: username.value,
  74. password: password.value,
  75. };
  76. const updateLoginTypeParams = {};
  77. updateLoginTypeParams.email = username.value;
  78. updateLoginTypeParams.loginType = "regular";
  79. gp.$showLoading($q);
  80. api
  81. .put("/api/public/login/type", updateLoginTypeParams)
  82. .then((res) => {
  83. // if succeed updating login type to regular
  84. if (res.data.ok == true) {
  85. // could succeed updating login type, do real login
  86. api
  87. .post("/login", Qs.stringify(data), {
  88. headers: {
  89. "Content-Type": "application/x-www-form-urlencoded",
  90. },
  91. })
  92. .then((response) => {
  93. gp.$generalNotify($q, true, "Login succeed!");
  94. location.reload();
  95. })
  96. .catch((e) => {
  97. gp.$generalNotify($q, false, "Fail login error ");
  98. gp.$hideLoading($q);
  99. console.log("Fail login error message: ");
  100. console.log(e);
  101. });
  102. } else {
  103. // could fail due to user not exist
  104. gp.$generalNotify($q, false, res.data.message);
  105. gp.$hideLoading($q);
  106. }
  107. })
  108. .catch((err) => {
  109. // could fail for unknown reason
  110. gp.$generalNotify($q, false, "Fail updating login type");
  111. console.log("Fail updating login type err:");
  112. console.log(err);
  113. gp.$hideLoading($q);
  114. });
  115. } else {
  116. gp.$generalNotify($q, false, "Not valid input");
  117. }
  118. }
  119. function whoami() {
  120. api
  121. .get("/api/v1/user/whoami")
  122. .then(function (response) {
  123. console.log("current user in setup: " + response.data);
  124. gp.$goPage("/");
  125. })
  126. .catch(function (error) {
  127. console.log("currently not logged in setup: " + error);
  128. });
  129. }
  130. function goToFacebookLogin() {
  131. FB.login(
  132. function (response) {
  133. // handle the response
  134. if (response.authResponse) {
  135. var accessToken = response.authResponse.accessToken;
  136. // get my info and set
  137. FB.api(
  138. "/me",
  139. "GET",
  140. { fields: "id,name,email,picture" },
  141. function (response) {
  142. // try login using the oauth way
  143. const params = {};
  144. params.accessToken = accessToken;
  145. params.facebookId = response.id;
  146. params.email = response.email;
  147. params.name = response.name;
  148. params.profileImageUrl = response.picture.data.url;
  149. var emailStore = response.email;
  150. api
  151. .post("/api/public/login/prereg-facebook", params)
  152. .then((res) => {
  153. var data = {
  154. username: emailStore,
  155. password: accessToken,
  156. };
  157. gp.$showLoading($q);
  158. api
  159. .post("/login", Qs.stringify(data), {
  160. headers: {
  161. "Content-Type": "application/x-www-form-urlencoded",
  162. },
  163. })
  164. .then((response) => {
  165. gp.$hideLoading($q);
  166. location.reload();
  167. })
  168. .catch((e) => {
  169. gp.$hideLoading($q);
  170. gp.$generalNotify($q, false, "Error message: " + e);
  171. });
  172. })
  173. .catch((err) => {
  174. gp.$hideLoading(false);
  175. gp.$generalNotify(
  176. $q,
  177. false,
  178. "Fail pre register facebook user"
  179. );
  180. console.log("Fail pre register facebook user");
  181. console.log(err);
  182. });
  183. }
  184. );
  185. } else {
  186. console.log("User cancelled login or did not fully authorize.");
  187. gp.$generalNotify(
  188. $q,
  189. false,
  190. "User cancelled login or did not fully authorize."
  191. );
  192. }
  193. },
  194. { scope: "email" }
  195. );
  196. }
  197. function initFacebookSDK() {
  198. // Load the Facebook SDK asynchronously
  199. const loadFacebookSDK = new Promise((resolve) => {
  200. window.fbAsyncInit = function () {
  201. FB.init({
  202. appId: "994181467335802",
  203. autoLogAppEvents: true,
  204. xfbml: true,
  205. version: "v13.0",
  206. });
  207. resolve();
  208. };
  209. });
  210. // Load the SDK script
  211. (function (d, s, id) {
  212. var js,
  213. fjs = d.getElementsByTagName(s)[0];
  214. console.log("js");
  215. console.log(js);
  216. console.log("fjs");
  217. console.log(fjs);
  218. if (d.getElementById(id)) {
  219. return;
  220. }
  221. js = d.createElement(s);
  222. js.id = id;
  223. js.src = "https://connect.facebook.net/en_US/sdk.js";
  224. fjs.parentNode.insertBefore(js, fjs);
  225. })(document, "script", "facebook-jssdk");
  226. // Wait for the SDK to be loaded
  227. loadFacebookSDK.then(() => {
  228. console.log("facebook sdk loaded");
  229. // You can now use the Facebook SDK methods here
  230. // For example, authenticate the user, fetch user data, etc.
  231. });
  232. }
  233. onMounted(() => {
  234. whoami();
  235. });
  236. onBeforeMount(() => {
  237. initFacebookSDK();
  238. });
  239. return {
  240. login,
  241. goToFacebookLogin,
  242. username,
  243. password,
  244. };
  245. },
  246. };
  247. </script>
  248. <style lang="sass" scoped>
  249. .login-border
  250. border-radius: 10px
  251. </style>