api login
This commit is contained in:
parent
1cbd9e7c26
commit
5a152350be
3 changed files with 72 additions and 28 deletions
54
src/controllers/LoginController.ts
Normal file
54
src/controllers/LoginController.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
import { Controller, Route, Tags, SuccessResponse, Response, Post, Body } from "tsoa";
|
||||||
|
import HttpSuccess from "../interfaces/http-success";
|
||||||
|
import HttpStatus from "../interfaces/http-status";
|
||||||
|
import CallAPI from "../interfaces/call-api";
|
||||||
|
import HttpError from "../interfaces/http-error";
|
||||||
|
|
||||||
|
@Route("api/v1/org/login")
|
||||||
|
@Tags("Profile")
|
||||||
|
@Response(
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
||||||
|
)
|
||||||
|
@SuccessResponse(HttpStatus.OK, "สำเร็จ")
|
||||||
|
export class LoginController extends Controller {
|
||||||
|
/**
|
||||||
|
* API login
|
||||||
|
*
|
||||||
|
* @summary - login
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Post()
|
||||||
|
async login(
|
||||||
|
@Body()
|
||||||
|
body: {
|
||||||
|
username: string;
|
||||||
|
password: string;
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
const data = {
|
||||||
|
client_id: "gettoken",
|
||||||
|
client_secret: process.env.AUTH_ACCOUNT_SECRET,
|
||||||
|
grant_type: "password",
|
||||||
|
requested_token_type: "urn:ietf:params:oauth:token-type:refresh_token",
|
||||||
|
username: body.username,
|
||||||
|
password: body.password,
|
||||||
|
};
|
||||||
|
let _data: any = null;
|
||||||
|
await Promise.all([
|
||||||
|
await new CallAPI()
|
||||||
|
.PostDataKeycloak("/realms/bma-ehr/protocol/openid-connect/token", data)
|
||||||
|
.then(async (x) => {
|
||||||
|
_data = x;
|
||||||
|
})
|
||||||
|
.catch(async (x) => {
|
||||||
|
throw new HttpError(HttpStatus.UNAUTHORIZED, "ชื่อผู้ใช้งานหรือรหัสผ่านไม่ถูกต้อง");
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
if (_data == null) {
|
||||||
|
return new HttpError(HttpStatus.UNAUTHORIZED, "ชื่อผู้ใช้งานหรือรหัสผ่านไม่ถูกต้อง");
|
||||||
|
} else {
|
||||||
|
return new HttpSuccess(_data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,34 +1,8 @@
|
||||||
import {
|
import { Controller, Route, Security, Tags, SuccessResponse, Response, Get } from "tsoa";
|
||||||
Controller,
|
|
||||||
Post,
|
|
||||||
Put,
|
|
||||||
Delete,
|
|
||||||
Route,
|
|
||||||
Security,
|
|
||||||
Tags,
|
|
||||||
Body,
|
|
||||||
Path,
|
|
||||||
Request,
|
|
||||||
SuccessResponse,
|
|
||||||
Response,
|
|
||||||
Get,
|
|
||||||
Query,
|
|
||||||
Example,
|
|
||||||
} from "tsoa";
|
|
||||||
import { AppDataSource } from "../database/data-source";
|
import { AppDataSource } from "../database/data-source";
|
||||||
import HttpSuccess from "../interfaces/http-success";
|
import HttpSuccess from "../interfaces/http-success";
|
||||||
import HttpStatus from "../interfaces/http-status";
|
import HttpStatus from "../interfaces/http-status";
|
||||||
import HttpError from "../interfaces/http-error";
|
|
||||||
import { Profile, CreateProfile, UpdateProfile, ProfileHistory } from "../entities/Profile";
|
|
||||||
import { Brackets, IsNull, Like, Not } from "typeorm";
|
|
||||||
import { OrgRevision } from "../entities/OrgRevision";
|
|
||||||
import { PosMaster } from "../entities/PosMaster";
|
|
||||||
import { PosLevel } from "../entities/PosLevel";
|
|
||||||
import { PosType } from "../entities/PosType";
|
|
||||||
import { calculateRetireDate, calculateRetireYear } from "../interfaces/utils";
|
|
||||||
import { RequestWithUser } from "../middlewares/user";
|
|
||||||
import { BloodGroup } from "../entities/BloodGroup";
|
import { BloodGroup } from "../entities/BloodGroup";
|
||||||
import { EducationLevel } from "../entities/EducationLevel";
|
|
||||||
import { Gender } from "../entities/Gender";
|
import { Gender } from "../entities/Gender";
|
||||||
import { Prefixe } from "../entities/Prefixe";
|
import { Prefixe } from "../entities/Prefixe";
|
||||||
import { Relationship } from "../entities/Relationship";
|
import { Relationship } from "../entities/Relationship";
|
||||||
|
|
@ -45,7 +19,6 @@ import { Rank } from "../entities/Rank";
|
||||||
@SuccessResponse(HttpStatus.OK, "สำเร็จ")
|
@SuccessResponse(HttpStatus.OK, "สำเร็จ")
|
||||||
export class MainController extends Controller {
|
export class MainController extends Controller {
|
||||||
private bloodGroupRepo = AppDataSource.getRepository(BloodGroup);
|
private bloodGroupRepo = AppDataSource.getRepository(BloodGroup);
|
||||||
private educationLevelRepo = AppDataSource.getRepository(EducationLevel);
|
|
||||||
private genderRepo = AppDataSource.getRepository(Gender);
|
private genderRepo = AppDataSource.getRepository(Gender);
|
||||||
private prefixeRepo = AppDataSource.getRepository(Prefixe);
|
private prefixeRepo = AppDataSource.getRepository(Prefixe);
|
||||||
private relationshipRepo = AppDataSource.getRepository(Relationship);
|
private relationshipRepo = AppDataSource.getRepository(Relationship);
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,23 @@ class CallAPI {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//Post
|
||||||
|
public async PostDataKeycloak(@Path() path: any, sendData: any) {
|
||||||
|
// const token = request.headers.authorization;
|
||||||
|
const url = process.env.KC_URL + path;
|
||||||
|
try {
|
||||||
|
const response = await axios.post(url, sendData, {
|
||||||
|
headers: {
|
||||||
|
// Authorization: `${token}`,
|
||||||
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
|
api_key: process.env.API_KEY,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return response.data;
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default CallAPI;
|
export default CallAPI;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue