cleaned up auth

This commit is contained in:
Win 2024-07-11 09:47:07 +07:00
parent d2a7b41a81
commit e6a2ba6861
3 changed files with 36 additions and 20 deletions

View File

@ -1,4 +1,3 @@
const express = require('express');
const axios = require('axios'); const axios = require('axios');
let auth_status = 0; let auth_status = 0;
@ -23,17 +22,18 @@ function verify_auth_status() {
} }
async function get_access_token(host, session_id) { async function get_access_token(host, session_id) {
axios.post(`${host}/api/miauth/${session_id}/check`) const response = await axios.post(`${host}/api/miauth/${session_id}/check`, null, {
.then((data) => { headers: {
const response = data.json(); 'Content-Type': 'application/json'
}
})
if(response.ok == true) { if(response.data.ok == true) {
console.log(response.token); console.log("Successfully retrieved access token");
return response.token; return response.data.token;
} else { } else {
return false; return false;
} }
});
}; };
module.exports.check_auth_status = check_auth_status; module.exports.check_auth_status = check_auth_status;

View File

@ -1,16 +1,31 @@
const axios = require('axios'); const axios = require('axios');
const fs = require('fs');
async function create_image(host, access_token, file_path) { async function create_image(host, access_token, file_path) {
axios.post(`${host}/api/drive/files/create`, { const create = axios.post(`${host}/api/drive/files/create`, {
file: fs.createReadStream(file_path) file: fs.createReadStream(file_path)
}, { }, {
headers: { headers: {
'Content-Type': 'multipart/form-data', 'Content-Type': 'multipart/form-data',
'Authorization': `Bearer ${access_token}` 'Authorization': `Bearer ${access_token}`
} }
}).then((data) => {
return data;
}); });
return create;
}; };
async function create_note(host, access_token, text, media_ids, visibility) {
axios.post(`${host}/api/notes/create`, {
text: text,
mediaIds: media_ids,
visibility: visibility
}, {
headers: {
'Authorization': `Bearer ${access_token}`
}
}).then((data) => {
return data.json();
});
}
module.exports.create_image = create_image; module.exports.create_image = create_image;

View File

@ -1,17 +1,15 @@
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import fs, { createReadStream } from 'node:fs'; import fs from 'node:fs';
import path from 'node:path'; import path from 'node:path';
import express from 'express'; import express from 'express';
import axios from 'axios';
import { fileFromPath } from 'formdata-node/file-from-path';
import * as Misskey from 'misskey-js'; import * as Misskey from 'misskey-js';
import * as Post from './api/post.cjs'; import * as Post from './api/post.cjs';
import * as Auth from './api/auth.cjs'; import * as Auth from './api/auth.cjs';
const __dirname = path.resolve(); const __dirname = path.resolve();
const config = JSON.parse(fs.readFileSync(path.resolve("config", "config.json"))); const config = JSON.parse(fs.readFileSync(path.join(__dirname, '/config/config.json')));
const token_file = JSON.parse(fs.readFileSync(path.resolve("config", "token.json"))); const token_file = JSON.parse(fs.readFileSync(path.join(__dirname, '/config/token.json')));
let app = express(); let app = express();
let port = process.env.PORT || 4000; let port = process.env.PORT || 4000;
@ -60,7 +58,10 @@ app.get('/callback', function(req, res) {
}); });
let static_image_path = path.join(__dirname, '/data/current_radar.jpg'); let static_image_path = path.join(__dirname, '/data/current_radar.jpg');
Post.create_image(host, access_token, static_image_path); const upload_image = await Post.create_image(host, access_token, static_image_path);
console.log(upload_image);
})(); })();
app.listen(port); app.listen(port);