|
|
@@ -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
|
+}
|