LoginPage.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. {{ $t("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="$t('enter_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="$t('enter_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="$t('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 { useI18n } from "vue-i18n";
  57. import { api } from "boot/axios";
  58. import Qs from "qs";
  59. export default {
  60. name: "LoginPage",
  61. setup() {
  62. const { locale, $t } = useI18n();
  63. const instance = getCurrentInstance();
  64. const app = getCurrentInstance().appContext.app;
  65. const gp = app.config.globalProperties;
  66. const $q = useQuasar();
  67. const username = ref("");
  68. const password = ref("");
  69. function login() {
  70. const isValidPassword = instance.refs.passwordInput.validate();
  71. const isValidUsername = instance.refs.usernameInput.validate();
  72. if (isValidUsername && isValidPassword) {
  73. // do real login logic
  74. var data = {
  75. username: username.value,
  76. password: password.value,
  77. };
  78. const updateLoginTypeParams = {};
  79. updateLoginTypeParams.email = username.value;
  80. updateLoginTypeParams.loginType = "regular";
  81. gp.$showLoading($q);
  82. api
  83. .put("/api/public/login/type", updateLoginTypeParams)
  84. .then((res) => {
  85. // if succeed updating login type to regular
  86. if (res.data.ok == true) {
  87. // could succeed updating login type, do real login
  88. api
  89. .post("/login", Qs.stringify(data), {
  90. headers: {
  91. "Content-Type": "application/x-www-form-urlencoded",
  92. },
  93. })
  94. .then((response) => {
  95. gp.$generalNotify($q, true, "Login succeed!");
  96. location.reload();
  97. })
  98. .catch((e) => {
  99. gp.$generalNotify($q, false, "Fail login error ");
  100. gp.$hideLoading($q);
  101. console.log("Fail login error message: ");
  102. console.log(e);
  103. });
  104. } else {
  105. // could fail due to user not exist
  106. gp.$generalNotify($q, false, res.data.message);
  107. gp.$hideLoading($q);
  108. }
  109. })
  110. .catch((err) => {
  111. // could fail for unknown reason
  112. gp.$generalNotify($q, false, "Fail updating login type");
  113. console.log("Fail updating login type err:");
  114. console.log(err);
  115. gp.$hideLoading($q);
  116. });
  117. } else {
  118. gp.$generalNotify($q, false, "Not valid input");
  119. }
  120. }
  121. function whoami() {
  122. api
  123. .get("/api/v1/user/whoami")
  124. .then(function (response) {
  125. console.log("current user in setup: " + response.data);
  126. gp.$goPage("/");
  127. })
  128. .catch(function (error) {
  129. console.log("currently not logged in setup: " + error);
  130. });
  131. }
  132. function goToFacebookLogin() {
  133. FB.login(
  134. function (response) {
  135. // handle the response
  136. if (response.authResponse) {
  137. var accessToken = response.authResponse.accessToken;
  138. // get my info and set
  139. FB.api(
  140. "/me",
  141. "GET",
  142. { fields: "id,name,email,picture" },
  143. function (response) {
  144. // try login using the oauth way
  145. const params = {};
  146. params.accessToken = accessToken;
  147. params.facebookId = response.id;
  148. params.email = response.email;
  149. params.name = response.name;
  150. params.profileImageUrl = response.picture.data.url;
  151. var emailStore = response.email;
  152. api
  153. .post("/api/public/login/prereg-facebook", params)
  154. .then((res) => {
  155. var data = {
  156. username: emailStore,
  157. password: accessToken,
  158. };
  159. gp.$showLoading($q);
  160. api
  161. .post("/login", Qs.stringify(data), {
  162. headers: {
  163. "Content-Type": "application/x-www-form-urlencoded",
  164. },
  165. })
  166. .then((response) => {
  167. gp.$hideLoading($q);
  168. location.reload();
  169. })
  170. .catch((e) => {
  171. gp.$hideLoading($q);
  172. gp.$generalNotify($q, false, "Error message: " + e);
  173. });
  174. })
  175. .catch((err) => {
  176. gp.$hideLoading(false);
  177. gp.$generalNotify(
  178. $q,
  179. false,
  180. "Fail pre register facebook user"
  181. );
  182. console.log("Fail pre register facebook user");
  183. console.log(err);
  184. });
  185. }
  186. );
  187. } else {
  188. console.log("User cancelled login or did not fully authorize.");
  189. gp.$generalNotify(
  190. $q,
  191. false,
  192. "User cancelled login or did not fully authorize."
  193. );
  194. }
  195. },
  196. { scope: "email" }
  197. );
  198. }
  199. function initFacebookSDK() {
  200. // Load the Facebook SDK asynchronously
  201. const loadFacebookSDK = new Promise((resolve) => {
  202. window.fbAsyncInit = function () {
  203. FB.init({
  204. appId: "994181467335802",
  205. autoLogAppEvents: true,
  206. xfbml: true,
  207. version: "v13.0",
  208. });
  209. resolve();
  210. };
  211. });
  212. // Load the SDK script
  213. (function (d, s, id) {
  214. var js,
  215. fjs = d.getElementsByTagName(s)[0];
  216. console.log("js");
  217. console.log(js);
  218. console.log("fjs");
  219. console.log(fjs);
  220. if (d.getElementById(id)) {
  221. return;
  222. }
  223. js = d.createElement(s);
  224. js.id = id;
  225. js.src = "https://connect.facebook.net/en_US/sdk.js";
  226. fjs.parentNode.insertBefore(js, fjs);
  227. })(document, "script", "facebook-jssdk");
  228. // Wait for the SDK to be loaded
  229. loadFacebookSDK.then(() => {
  230. console.log("facebook sdk loaded");
  231. // You can now use the Facebook SDK methods here
  232. // For example, authenticate the user, fetch user data, etc.
  233. });
  234. }
  235. onMounted(() => {
  236. whoami();
  237. });
  238. onBeforeMount(() => {
  239. initFacebookSDK();
  240. });
  241. return {
  242. login,
  243. goToFacebookLogin,
  244. username,
  245. password,
  246. };
  247. },
  248. };
  249. </script>
  250. <style lang="sass" scoped>
  251. .login-border
  252. border-radius: 10px
  253. </style>