瀏覽代碼

signup error

tum 2 年之前
父節點
當前提交
3269cf184c

+ 93 - 9
src/composable/settings.ts

@@ -1,5 +1,6 @@
1 1
 import { CapacitorHttp } from '@capacitor/core';
2 2
 import Axios from "axios";
3
+//import axios from "axios";
3 4
 import { setupCache } from 'axios-cache-interceptor';
4 5
 
5 6
 
@@ -8,10 +9,13 @@ import { Preferences } from '@capacitor/preferences';
8 9
 
9 10
 export const TOKEN = '173cb9e357a861abd91e8008fab9246e0cc116af'
10 11
 //export const BASE_URL = 'http://192.168.1.35:8020/'
11
-//export const BASE_URL = 'http://localhost:8020/'
12
-export const BASE_URL = 'https://www.tigermuaythai.live/'
12
+export const BASE_URL = 'http://localhost:8020/'
13
+//export const BASE_URL = 'https://www.tigermuaythai.live/'
13 14
 
14 15
 const axios = setupCache(Axios); 
16
+//
17
+//axios.defaults.xsrfHeaderName = "X-CSRFTOKEN";
18
+//axios.defaults.xsrfCookieName = "csrftoken";
15 19
 
16 20
 export const getProducts = async () => {
17 21
   console.log("Get Products")
@@ -37,15 +41,40 @@ export const getObject = async () => {
37 41
 
38 42
 export const getToken = async () => {
39 43
   console.log("get token ")
40
-  const { data } = await axios.post(BASE_URL + "api-token-auth/", {username: 'root', password: 'Tum 1984'})
41
-  console.log("get token ", data)
42
-  await Preferences.set({
44
+  const token = await Preferences.get({ key: 'token' });
45
+  console.log(token)
46
+  if( token.value == null ) {
47
+    //const { data } = await axios.post(BASE_URL + "api-token-auth/", {username: 'root', password: 'Tum 1984'})
48
+    const { data } = await axios.post(BASE_URL + "dj-rest-auth/login/", {username: 'root', password: 'Tum 1984'})
49
+    console.log("get token ", data)
50
+    await Preferences.set({
51
+      key: 'token', 
52
+      value: data.token
53
+    })
54
+    return data.token
55
+  }else {
56
+    return token.value
57
+  }
58
+}
59
+export const initAPI = async () => {
60
+  const r = await axios.get(BASE_URL + "backend/api/")
61
+  console.log("--- init api ---")
62
+  console.log(r)
63
+  console.log("-- cookie ---")
64
+  const cookieHeaders = r.headers['Set-Cookie'];
65
+  console.log(cookieHeaders)
66
+}
67
+export const setUserToken = async(token) => {
68
+  let data = await Preferences.set({
69
+    key: 'user_token', 
70
+    value: token
71
+  })
72
+  let data2 = await Preferences.set({
43 73
     key: 'token', 
44
-    value: data.token
74
+    value: token
45 75
   })
46
-  return data.token
76
+  return data
47 77
 }
48
-
49 78
 export const getTrainers = async () => {
50 79
   const token = await Preferences.get({ key: 'token' });
51 80
   console.log("token = ", token)
@@ -81,7 +110,7 @@ export const listMats = async  (ids=[]) => {
81 110
   }
82 111
 }
83 112
 export const listCourses = async  () => {
84
-
113
+  console.log("list courses ... ")
85 114
   const token = await Preferences.get({ key: 'token' });
86 115
   console.log("token = ", token)
87 116
   const { data } = await axios.get(BASE_URL + "backend/api/courses/?ordering=-id&status=active", {
@@ -228,3 +257,58 @@ export const callUrl = async(url) => {
228 257
   })
229 258
   return data
230 259
 }
260
+export const facebookLogin = async(access_token) => {
261
+
262
+  const token = await Preferences.get({ key: 'token' });
263
+  console.log("access_code = ", access_token)
264
+  const { data } = await axios.post(BASE_URL + 'dj-rest-auth/facebook/',{access_token: access_token.token}, {
265
+    headers: {
266
+      'Content-Type': 'application/json', 
267
+    }
268
+  }).catch((error) => {
269
+    console.log("fb error = ", error)
270
+  })
271
+  console.log("facebook login = ", data)
272
+  return data
273
+}
274
+
275
+export const logout = async() => {
276
+
277
+  console.log("--- logout ---")
278
+  const token = await Preferences.get({ key: 'user_token' });
279
+  console.log("user_token = ", token)
280
+  //const { data } = await axios.post(BASE_URL + 'dj-rest-auth/logout/', {}, {
281
+    //headers: { 
282
+    //'Authorization': `Token ${token.value}`
283
+    //}
284
+  //})
285
+  await clearPref()
286
+  const { data } = await axios.post(BASE_URL + 'dj-rest-auth/logout/')
287
+  return data
288
+}
289
+export const clearPref = async() => {
290
+
291
+  const clear = await Preferences.clear()
292
+  return clear
293
+}
294
+export const isLogin = async() => {
295
+  const { value } = await Preferences.get({ key: 'user_token' });
296
+  if( value ) 
297
+    return true
298
+  else
299
+    return false
300
+}
301
+
302
+export const restSignup = async (username, email, pass1, pass2) => {
303
+  const { data } = await axios.post(BASE_URL + 'dj-rest-auth/registration/',{
304
+    username: username, 
305
+    email: email,
306
+    password1: pass1,
307
+    password2: pass2, 
308
+  }, {
309
+    headers: {
310
+      'Content-Type': 'application/json', 
311
+    }
312
+  })
313
+  return data
314
+}

+ 5 - 0
src/router/index.ts

@@ -52,6 +52,11 @@ const routes: Array<RouteRecordRaw> = [
52 52
         component: () => import('@/views/SearchPage.vue')
53 53
       },
54 54
       {
55
+        path: 'signup/',
56
+        name: 'signup', 
57
+        component: () => import('@/views/SignupPage.vue')
58
+      },
59
+      {
55 60
         path: 'post_detail/:id/',
56 61
         name: 'post_detail', 
57 62
         component: () => import('@/views/PostDetailPage.vue')

+ 9 - 2
src/views/CourseMatDetailPage.vue

@@ -45,7 +45,8 @@
45 45
 import { IonPage, IonHeader, IonToolbar, IonTitle, IonContent,   IonNavLink,
46 46
     IonButton,
47 47
     IonButtons,
48
-    IonBackButton, onIonViewWillEnter, IonRow, IonGrid, IonCol, IonItem, IonList, IonAvatar, IonLabel, IonItemDivider} from '@ionic/vue';
48
+    IonBackButton, onIonViewWillEnter, IonRow, IonGrid, IonCol, IonItem, IonList, IonAvatar, IonLabel, IonItemDivider,
49
+    useIonRouter } from '@ionic/vue';
49 50
 
50 51
 import { Autoplay, Keyboard, Pagination, Scrollbar, Zoom } from 'swiper/modules';
51 52
 import { Swiper, SwiperSlide } from 'swiper/vue';
@@ -60,7 +61,7 @@ import 'swiper/css/zoom';
60 61
 import '@ionic/vue/css/ionic-swiper.css';
61 62
 
62 63
 import { useRoute } from 'vue-router';
63
-import { getMat, BASE_URL, listMats } from '@/composable/settings';
64
+import { getMat, BASE_URL, listMats, isLogin} from '@/composable/settings';
64 65
 import { ref } from 'vue';
65 66
 import CourseMat from '@/components/CourseMat.vue'
66 67
 
@@ -73,12 +74,18 @@ const mat = ref()
73 74
 const name = ref()
74 75
 const mats = ref()
75 76
 
77
+const ionRouter = useIonRouter();
76 78
 
77 79
 onIonViewWillEnter(async () => {
80
+  const rl  = await isLogin()
81
+  if( rl == false ) {
82
+    ionRouter.navigate('/tabs/tab4', 'forward', 'replace');
83
+  }
78 84
   mat.value = await getMat(id)
79 85
   console.log(mat.value)
80 86
   name.value = mat.value.vimeo_name || mat.value.course_name
81 87
   mats.value  = await listMats(mat.value.related_mats)
88
+
82 89
 })
83 90
 </script>
84 91
 

+ 18 - 1
src/views/ProfilePage.vue

@@ -7,6 +7,9 @@
7 7
     </ion-header>
8 8
     <ion-content>
9 9
       <div class="example-content">Listen now content</div>
10
+      <ion-button @click='clear'>Clear Pref</ion-button>
11
+      <ion-button @click='btnLogout'>Btn Logout</ion-button>
12
+      <ion-button router-link="/tabs/signup">Signup</ion-button>
10 13
       <template v-if="isLogin">
11 14
       <ion-button @click='fbLogout'>Logout</ion-button>
12 15
       </template>
@@ -23,6 +26,7 @@ import { IonPage, IonHeader, IonToolbar, IonTitle, IonContent, IonButton, onIonV
23 26
 import ExploreContainer from '@/components/ExploreContainer.vue';
24 27
 import { defineComponent, onMounted } from 'vue';
25 28
 import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';
29
+import { facebookLogin, setUserToken, logout, clearPref } from '@/composable/settings';
26 30
 
27 31
 onMounted(() => {
28 32
   GoogleAuth.initialize();
@@ -57,8 +61,13 @@ const fbLogin = async () => {
57 61
   const result = await (<FacebookLoginResponse>(
58 62
     FacebookLogin.login({ permissions: FACEBOOK_PERMISSIONS })
59 63
   ));
60
-
64
+  console.log("-- fbLogin --")
65
+  console.info(result.accessToken)
61 66
   if (result.accessToken) {
67
+    let d = await facebookLogin(result.accessToken)
68
+    console.log("facebook authen ", d)
69
+    await setUserToken(d.key)
70
+    
62 71
     // Login successful.
63 72
     isLogin.value = true
64 73
     console.log(`Facebook access token is ${result.accessToken.token}`);
@@ -67,6 +76,11 @@ const fbLogin = async () => {
67 76
 const fbLogout = async () => {
68 77
   await FacebookLogin.logout();
69 78
   isLogin.value = false
79
+  await logout()
80
+}
81
+const btnLogout = async () => {
82
+  isLogin.value = false
83
+  await logout()
70 84
 }
71 85
 onIonViewWillEnter(async () => {
72 86
   const result = await (<FacebookLoginResponse>(
@@ -103,4 +117,7 @@ const ggCheckLogin = async() => {
103 117
             }
104 118
         });
105 119
 }
120
+const clear = async() => {
121
+  await clearPref()
122
+}
106 123
 </script>

+ 111 - 0
src/views/SignupPage.vue

@@ -0,0 +1,111 @@
1
+<template>
2
+  <ion-page>
3
+    <ion-header>
4
+      <ion-toolbar>
5
+        <ion-buttons slot="start">
6
+          <ion-back-button></ion-back-button>
7
+        </ion-buttons>
8
+        <ion-title>
9
+          Signup
10
+        </ion-title>
11
+      </ion-toolbar>
12
+    </ion-header>
13
+    <ion-content class='ion-padding'>
14
+      <ion-input label="Username"  ref="username" label-placement="floating" fill="outline" placeholder="Example, john"
15
+      error-text="Help me" 
16
+        ></ion-input>
17
+      <ion-list>
18
+        <ion-item>
19
+          <ion-input label="Email" label-placement="floating" ref='email' fill="outline" type="email" placeholder="Example, hello@gmail.com"></ion-input>
20
+        </ion-item>
21
+        <ion-item>
22
+          <ion-input label="Password" label-placement='floating' fill='outline' ref="password" type="password" placeholder="Password"></ion-input>
23
+        </ion-item>
24
+        <ion-item>
25
+          <ion-input label="Password Confirmation" label-placement='floating' fill='outline' ref="password2" type="password" placeholder="Password Confirmation"></ion-input>
26
+        </ion-item>
27
+      </ion-list>
28
+      <div>
29
+      <ion-button @click="signup" class=' ion-margin-top'>Signup</ion-button>
30
+      </div>
31
+      <br>
32
+       <ion-input
33
+    ref="input"
34
+    type="email"
35
+    fill="solid"
36
+    label="Email"
37
+    label-placement="floating"
38
+    helper-text="Enter a valid email"
39
+    error-text="Invalid email"
40
+    @ionInput="validate"
41
+    @ionBlur="markTouched"
42
+  ></ion-input>
43
+    </ion-content>
44
+  </ion-page>
45
+</template>
46
+
47
+<script setup lang="ts">
48
+import { IonPage, IonHeader, IonToolbar, IonTitle, IonContent, IonButton, onIonViewWillEnter, IonList, IonSearchbar, IonLabel,
49
+IonItem, IonBackButton, IonProgressBar, IonButtons, IonInfiniteScroll,
50
+    IonInfiniteScrollContent, InfiniteScrollCustomEvent, IonInput } from '@ionic/vue';
51
+import { defineComponent, onMounted } from 'vue';
52
+import { GoogleAuth } from '@codetrix-studio/capacitor-google-auth';
53
+import { searchMat, callUrl,restSignup } from '@/composable/settings';
54
+
55
+import { ref } from 'vue';
56
+import SearchItem from '@/components/SearchItem.vue';
57
+
58
+const results = ref();
59
+const is_empty = ref(false);
60
+const processing = ref(false)
61
+let next_url = null
62
+
63
+const username = ref(null)
64
+const password = ref(null)
65
+const password2 = ref(null)
66
+const email = ref(null)
67
+const input = ref(null)
68
+
69
+const signup = async () => { 
70
+  username.value.$el.errorText = "No way"
71
+  username.value.$el.classList.remove('ion-valid')
72
+  username.value.$el.classList.remove('ion-invalid')
73
+  username.value.$el.classList.add('ion-invalid')
74
+  username.value.$el.classList.add('ion-touched');
75
+  try {
76
+    console.log("username = ", username.value.$el.value)
77
+    const data = await restSignup(username.value.$el.value, email.value.$el.value, password.value.$el.value, password2.value.$el.value)
78
+    console.log(data)
79
+  }catch( err ) {
80
+    console.log("-- error --")
81
+    console.log(err)
82
+    username.value.$el.classList.add('ion-invalid')
83
+  }
84
+}
85
+
86
+const validateEmail = (email) => {
87
+        return email.match(
88
+          /^(?=.{1,254}$)(?=.{1,64}@)[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
89
+        );
90
+      }
91
+
92
+  const validate = (ev) => {
93
+        const value = ev.target.value;
94
+
95
+        input.value.$el.classList.remove('ion-valid');
96
+        input.value.$el.classList.remove('ion-invalid');
97
+
98
+        if (value === '') return;
99
+
100
+        validateEmail(value)
101
+          ? input.value.$el.classList.add('ion-valid')
102
+          : input.value.$el.classList.add('ion-invalid');
103
+      }
104
+
105
+const markTouched = () => {
106
+  input.value.$el.classList.add('ion-touched');
107
+}
108
+const markTouched2 = () => {
109
+  username.value.$el.classList.add('ion-touched');
110
+}
111
+</script>

+ 9 - 9
src/views/Tab1Page.vue

@@ -67,7 +67,7 @@ import SearchEngine from '@/components/SearchEngine.vue'
67 67
 import { CapacitorHttp } from '@capacitor/core';
68 68
 import { ref } from 'vue';
69 69
 import { TOKEN, getProducts, setToken, getObject, getToken, getTrainers, listMats, storeAPNToken,
70
-getTodayProgs, getLive, getPosts} from '@/composable/settings';
70
+getTodayProgs, getLive, getPosts, initAPI} from '@/composable/settings';
71 71
 //import VueCoreVideoPlayer from 'vue-core-video-player'
72 72
 import { vueVimeoPlayer } from 'vue-vimeo-player'
73 73
 
@@ -109,6 +109,7 @@ const options = {
109 109
 
110 110
   //const player = new Player(video, options);
111 111
 
112
+const liveObj = ref()
112 113
 const modules =  [Autoplay, Keyboard, Pagination, Scrollbar, Zoom]
113 114
   onIonViewWillEnter(async () => {
114 115
     //console.log("video " ,video)
@@ -126,7 +127,7 @@ const modules =  [Autoplay, Keyboard, Pagination, Scrollbar, Zoom]
126 127
     // setToken()
127 128
     // let u = getObject()
128 129
     //console.log('u ', u)
129
-
130
+    await initAPI()
130 131
     token.value = await getToken()
131 132
     getTrainers().then( (data) => {
132 133
       trainers.value = data
@@ -146,6 +147,12 @@ const modules =  [Autoplay, Keyboard, Pagination, Scrollbar, Zoom]
146 147
     getTodayProgs().then( (data) => {
147 148
       todays.value = data
148 149
     })
150
+    setInterval( async () => {
151
+      console.log("did enter")
152
+      if( liveObj.value == null ||  liveObj.value.result == false ) {
153
+        liveObj.value = await getLive()
154
+      }
155
+    }, 10000)
149 156
   })
150 157
 
151 158
   onIonViewWillLeave(() => {
@@ -153,14 +160,7 @@ const modules =  [Autoplay, Keyboard, Pagination, Scrollbar, Zoom]
153 160
 
154 161
   })
155 162
 
156
-const liveObj = ref()
157 163
   onIonViewDidEnter(async() => {
158
-    setInterval( async () => {
159
-      console.log("did enter")
160
-      if( liveObj.value == null ||  liveObj.value.result == false ) {
161
-        liveObj.value = await getLive()
162
-      }
163
-    }, 10000)
164 164
   })
165 165
 
166 166
   const doGet = async () => {

+ 7 - 2
src/views/Tab3Page.vue

@@ -3,6 +3,11 @@
3 3
     <ion-header>
4 4
       <ion-toolbar>
5 5
         <ion-title>Schedule</ion-title>
6
+       <ion-buttons slot="end">
7
+          <ion-button :router-link="'/tabs/search_page/'">
8
+            <ion-icon slot="icon-only" :icon="search"></ion-icon>
9
+          </ion-button>
10
+        </ion-buttons>
6 11
       </ion-toolbar>
7 12
     </ion-header>
8 13
     <ion-content>
@@ -76,8 +81,8 @@
76 81
 
77 82
 <script setup lang="ts">
78 83
 import { IonPage, IonHeader, IonToolbar, IonTitle, IonContent, IonIcon, IonSegment, IonSegmentButton, onIonViewWillEnter, IonGrid, 
79
-IonRow, IonCol } from '@ionic/vue';
80
-import { home, heart, pin, star, call, globe, basket, barbell, trash, person } from 'ionicons/icons';
84
+IonRow, IonCol, IonButton, IonButtons } from '@ionic/vue';
85
+import { home, heart, pin, star, call, globe, basket, barbell, trash, person, search } from 'ionicons/icons';
81 86
 import ExploreContainer from '@/components/ExploreContainer.vue';
82 87
 import CourseSchedule from '@/components/CourseSchedule.vue';
83 88
 import { ref } from 'vue';